Structure of a C# Program

Learn about how C# programs are structured.

Statements and code blocks

C# code files are text files with .cs extension. They contain instructions that’re compiled to ILIntermediate Language code.

Statements are the basic building blocks of C# source code. A statement can be some action, such as an arithmetic operation, a method invocation, or a variable declaration and assignment.

Console.WriteLine("This is a statement.");

Like in C and C++, every statement must be followed by a semicolon (;). Leaving them out results in compilation errors, and our program won’t compile to IL.

We can combine a set of instructions into blocks of code. To create a code block, we enclose our statements in curly braces.

{
	Console.WriteLine("I am inside a block!");
	Console.WriteLine("Me too.");
}

Blocks can contain other blocks. Essentially, both a class and a method can be seen as code blocks. A class contains a method, and a method contains instructions within its curly braces.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy