Here is a program that multiplies 12 x 30, and prints the result, 360, to the screen. The double-forward slash indicates that the remainder of a line is a comment.
using System; // importing namespace class Test // class declaration { static void Main( ) // method declaration { int x = 12 * 30; // statement 1 Console.WriteLine (x); // statement 2 } // end of method } // end of class
At the heart of this program lie two statements. Statements in C# execute sequentially. Each statement is terminated by a semicolon:
int x = 12 * 30; Console.WriteLine (x);
The first statement computes the expression 12 * 30 and stores the
result in a local variable, named x
,
which 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.
A method performs an action in a series of statements, called a
statement block—a pair of braces containing zero or more statements.
We defined a single method named Main
:
static void Main( ) { ... }
Writing higher-level functions that call upon lower-level functions simplifies a program. We can refactor our program with a reusable method that multiplies an integer by 12 as follows:
using System; class Test { static void Main( ) { Console.WriteLine (FeetToInches (30)); // 360 Console.WriteLine (FeetToInches (100)); // 1200 } static int FeetToInches (int feet) { int inches = feet * 12; return inches; } }
A method can receive input data from the caller by specifying
parameters, and output data back to the caller
by specifying a return type. We defined a method called FeetToInches
that has a parameter for inputting feet, and a
return type for outputting inches:
static int FeetToInches (int feet) {...}
The literals
30
and 100
are the
arguments passed to the FeetToInches
method. The Main
method in our
example has empty parentheses because it has no parameters, and it is void
because it doesn’t return any value to its caller:
staticvoid
Main()
C# recognizes a method called Main
as signaling the
default entry point of execution. The Main
method may
optionally return an integer (rather than void
) to return
a value to the execution environment. The Main
method can
also optionally take an array of string arguments (that will be populated with any arguments
passed to the executable). For example:
static int Main (string[] args) {...}
An array (such as string[]
) represents a fixed
number of elements of a particular type (see the upcoming “Arrays,” section).
Methods are one of several kinds of functions in C#. Another kind of function we used was the * operator, used to perform multiplication. There are also constructors, properties, events, indexers, and finalizers.
In our example, the two methods are grouped into a class. A class
groups function members and data members to form an object-oriented building block. The
Console
class groups members that handle command-line
input/output functionality, such as the WriteLine
method.
Our Test
class groups two methods—the Main
method and the FeetToInches
method. A class is a kind of type, which we
will examine later in the “Type Basics” section.
At the outermost level of a program, types are organized into
namespaces. The using
directive
made the System
namespace available to our application,
so we could reference System.Console
without the System
. prefix. We could define all our classes within the
TestPrograms
namespace, as follows:
using System; namespace TestPrograms { class Test {...} class Test2 {...} }
The .NET Framework is organized into nested namespaces. For example, this is the namespace that contains types for handling text:
using System.Text;
The using
directive is there for convenience; you can
also refer to a type by its fully qualified name, which is the type name prefixed with its
namespace, such as System.Text. StringBuilder
.
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., and it can be either an application or a
library. A normal console or Windows application has a Main
method and is an .exe. A
library is a .dll, and is equivalent to an
.exe without an entry point. Its purpose is to be called upon
(referenced) by an application or by other libraries. The .NET Framework is a set of
libraries.
The name of the C# compiler is csc.exe. You can either use an IDE
such as Visual Studio .NET to call csc
automatically,
or compile manually from the command line. To compile manually, first save a program to a
file such as MyFirstProgram.cs, and then invoke csc
(located under
<windows>/Microsoft.NET/Framework)
from the command
line, as follows:
csc MyFirstProgram.cs
This produces an application named MyFirstProgram.exe.To produce a library (.dll), you’d do the following:
csc /target:library MyFirstProgram.cs