Here is a program that multiplies 12 by 30, and prints the result, 360, to the screen. The double forward slashes indicate 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 lies two statements. Statements in C# execute
sequentially. Each statement is terminated by a semicolon. 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
.
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, both of type int
(integer).
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 is void
because it doesn’t
return any value to its caller. 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
) in order to return a
value to the execution environment. The Main
method can also optionally accept an array
of strings as a parameter (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
section Arrays).
Methods are one of several kinds of functions in C#. Another kind of function we used was the
*
operator, 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 in Type Basics.
At the outermost level of a program, types are organized
into namespaces. The using
directive was used to make the System
namespace available to our application,
to use the Console
class. 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. 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. 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 to compile, or call csc
manually from the command line. To compile
manually, first save a program to a file such as MyFirstProgram.cs, and then go to the command line and invoke csc
(located under
%SystemRoot%\Microsoft.NET\Framework\<framework-version>,
where %SystemRoot% is
your Windows directory) as follows:
csc MyFirstProgram.cs
This produces an application named MyFirstProgram.exe.
To produce a library (.dll), do the following:
csc /target:library MyFirstProgram.cs