The programs and code snippets in this book mirror those in Chapters 2 through 4 of C# 9.0 in a Nutshell and are all available as interactive samples in LINQPad. Working through these samples in conjunction with the book accelerates learning in that you can edit the samples and instantly see the results without needing to set up projects and solutions in Visual Studio.
To download the samples, click the Samples tab in LINQPad and then click Download more samples. LINQPad is freego to www.linqpad.net.
A First C# Program
Following is a program that multiplies 12 by 30 and prints the result, 360, to the screen. The double forward slash indicates that the remainder of a line is a comment:
int x = 12 * 30; // Statement 1System.Console.WriteLine (x); // Statement 2
Our program consists of two statements. Statements in C# execute sequentially and are terminated by a semicolon. The first statement computes the expression12 * 30
and stores the result in a variable, named x
, whose type is a 32-bit integer (int
). The second statement calls the WriteLine
method on a class called Console
, which is defined in a namespace called System
. This prints the variable x
to a text window on the screen.
.
At the outermost level, types are organized into namespaces. Many commonly used typesincluding the Console
classreside in the System
namespace. The .NET libraries are organized into nested namespaces. For example, the System.Text
namespace contains types for handling text, and System.IO
contains types for input/output.
Qualifying the Console
class with the System
namespace on every use adds clutter. The using
directive lets you avoid this clutter by importing a namespace:
using System;
// Import the System namespaceint x = 12 * 30;
Console.WriteLine
(x); // No need to specify System
A basic form of code reuse is to write higher-level functions that call lower-level functions. We can refactor our program with a reusable method called FeetToInches
that multiplies an integer by 12, as follows:
using System;Console.WriteLine (
FeetToInches (30)
); // 360Console.WriteLine (
FeetToInches (100)
); // 1200
int FeetToInches (int feet)
{
int inches = feet * 12; return inches;
}
Our method contains a series of statements surrounded by a pair of braces. This is called a statement block.
A method can receive input data from the caller by specifying parameters and output data back to the caller by specifying a return type. Our FeetToInches
method has a parameter for inputting feet, and a return type for outputting inches:
int
FeetToInches (
int feet
)...
The literals30
and 100
are the arguments passed to the FeetToInches
method.
If a method doesnt receive input, use empty parentheses. If it doesnt return anything, use the void
keyword:
using System;SayHello();
void
SayHello
()
{ Console.WriteLine ("Hello, world");}
Methods are one of several kinds of functions in C#. Another kind of function we used in our example program was the *
operator, which performs multiplication. There are also constructors, properties, events, indexers, and finalizers.
Compilation
The C# compiler compiles source code (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 an entry point, whereas a library does not. The purpose of a library is to be called upon (referenced) by an application or by other libraries..NET 5 itself is a set of libraries (as well as a runtime environment).
Each of the programs in the preceding section began directly with a series of statements (called top-level statements). The presence of top-level statements implicitly creates an entry point for a console or Windows application. (Without top-level statements, a Main method denotes an applications entry pointsee .)
To invoke the compiler, you can either use an integrated development environment (IDE) such as Visual Studio or Visual Studio Code, or call it manually from the command line. To manually compile a console application with .NET, first download the .NET 5 SDK, and then create a new project, as follows:
dotnet new console -o MyFirstProgramcd MyFirstProgram