As mentioned, the class is C#’s basic unit of encapsulation. Example is the name of the class. The class definition begins with the opening curly brace ({) and ends with the closing curly brace (}). The elements between the two braces are members of the class.
The next line of code is shown here:
static void Main() {
This line begins the Main( ) method. As mentioned earlier, in C#, a subroutine is called a method. As the comment preceding it suggests, this is the line at which the program will begin executing. All C# applications begin execution by calling Main( ).
The line begins with the keyword static. A method that is modified by static can be called before an object of its class has been created. This is necessary because Main( ) is called at program startup. The keyword void indicates that Main( ) does not return a value.
The next line of code is shown here. Notice that it occurs inside Main( ).
Console.WriteLine(“A simple C# program.”);
This line outputs the string “A simple C# program.” followed by a new line on the screen. Output is actually accomplished by the built-in method WriteLine( ).
Second program
using System;
class Example2 {
static void Main() {
int x; // this declares a variable
int y; // this declares another variable
x = 100; // this assigns 100 to x
Console.WriteLine(“x contains ” + x);
y = x / 2;
Console.Write(“y contains x / 2: “);
Console.WriteLine(y);
}
}
Another example
using System;
class Example3 {
static void Main() {
int ivar; // this declares an int variable
double dvar; // this declares a floating-point variable
ivar = 100; // assign ivar the value 100
dvar = 100.0; // assign dvar the value 100.0
Console.WriteLine(“Original value of ivar: ” + ivar);
Console.WriteLine(“Original value of dvar: ” + dvar);
Console.WriteLine(); // print a blank line
// Now, divide both by 3.
ivar = ivar / 3;
dvar = dvar / 3.0;
Console.WriteLine(“ivar after division: ” + ivar);
Console.WriteLine(“dvar after division: ” + dvar);
}
}
The next line of code is shown here:
static void Main() {
This line begins the Main( ) method. As mentioned earlier, in C#, a subroutine is called a method. As the comment preceding it suggests, this is the line at which the program will begin executing. All C# applications begin execution by calling Main( ).
The line begins with the keyword static. A method that is modified by static can be called before an object of its class has been created. This is necessary because Main( ) is called at program startup. The keyword void indicates that Main( ) does not return a value.
The next line of code is shown here. Notice that it occurs inside Main( ).
Console.WriteLine(“A simple C# program.”);
This line outputs the string “A simple C# program.” followed by a new line on the screen. Output is actually accomplished by the built-in method WriteLine( ).
Second program
using System;
class Example2 {
static void Main() {
int x; // this declares a variable
int y; // this declares another variable
x = 100; // this assigns 100 to x
Console.WriteLine(“x contains ” + x);
y = x / 2;
Console.Write(“y contains x / 2: “);
Console.WriteLine(y);
}
}
Another example
using System;
class Example3 {
static void Main() {
int ivar; // this declares an int variable
double dvar; // this declares a floating-point variable
ivar = 100; // assign ivar the value 100
dvar = 100.0; // assign dvar the value 100.0
Console.WriteLine(“Original value of ivar: ” + ivar);
Console.WriteLine(“Original value of dvar: ” + dvar);
Console.WriteLine(); // print a blank line
// Now, divide both by 3.
ivar = ivar / 3;
dvar = dvar / 3.0;
Console.WriteLine(“ivar after division: ” + ivar);
Console.WriteLine(“dvar after division: ” + dvar);
}
}