CSharp/C# Tutorial - C# First Program






We are going to write our first C# program.

Here is a program that multiplies 2 by 3 and prints the result, 6, to the screen.

The double forward slash indicates that the remainder of a line is a comment.


using System; // Importing namespace 
//from w  w  w. jav a  2s .  c  o  m
class Test // Class declaration 
{ 
    static void Main() // Method declaration 
    {
       int x = 2 * 3; // Statement 1 
       Console.WriteLine (x); // Statement 2 
    } // End of method 
} // End of class 

At the heart of this program has two statements:


    int x = 2 * 3; 
    Console.WriteLine (x); 

Statements in C# execute sequentially and are terminated by a semicolon.

The first statement computes the expression 2 * 3 and stores the result in a local variable, named x, and x is an integer type.

The second statement calls the Console class's WriteLine method, to print the variable x to a text window on the screen.

Here Console is a class name while the WriteLine is the method name.

We pass in x as parameter.





Main Method

We defined a single method named Main:


static void Main() {
    ... 
} 

We can refactor our program with a reusable method that multiplies an integer by 2 as follows:


using System; /* w w  w . j  a va2  s  .  c o m*/

class Test { 
    static void Main() { 
        Console.WriteLine (OneMethod (3)); 
        Console.WriteLine (OneMethod (1)); 
    }
    static int OneMethod (int feet) 
    { 
        int inches = feet * 2; 
        return inches; 
    } 
} 

C# recognizes a method called Main as signaling the default entry point of execution.

The Main method can optionally return an integer rather than void in order to return a value to the execution environment.

The Main method can optionally accept an array of strings as a parameter.

For example:


static int Main (string[] args) {
   ...
} 

An array string[] represents a fixed number of elements of a particular type.





Compilation

The C# compiler compiles source code, specified as a set of files with the .cs extension, into an assembly.

An assembly is the unit of packaging and deployment in .NET.

An assembly can be either an application or a library.

A normal console or Windows application has a Main method and is an .exe file.

A library is a .dll and is equivalent to an .exe without an entry point.

The name of the C# compiler is csc.exe.

You can either use an IDE such as Visual Studio to compile, or call csc manually from the command line.

To compile manually, first save a program to a file such as Main.cs, and then go to the command line and invoke


csc 

which is under %SystemRoot%\Microsoft.NET\Framework\framework-version where %SystemRoot% is your Windows directory.

as follows:


csc Main.cs 

This produces an application named Main.exe.

To produce a library (.dll), do the following:


csc /target:library Main.cs 

Example

The following code outputs a message and read user input to exit.


using System; //w  w w  .j  ava2s .c  o  m

public class MyFirstClass 
{ 
   static void Main() 
   { 
      Console.WriteLine("Hello from Java2s.com."); 
      Console.ReadLine(); 
      return; 
   } 
}