C# Program Structure

This section describes C# program structure and basic rules. A C# program is user defined class with the 'public static void Main()' method as the execution entry point.

We have our first C# program written, compiled, and executed. Now let's look at the C# as a programming language. First, let's exam our first program to understand some basic rules about the structure of a C# program.

Rule 1: A C# program must have at least one class. Class is the basic unit of a C# program. We will learn more about class later in this book. In our first program, the following structure defines a class called "Hello":

class Hello {
   ...
}

Rule 2: A C# program must have an entry point, which must be defined as a "public", "static", and "void" method named as "Main" in a class. In our first program, this entry point is defined in the "Hello" class as the following:

class Hello {
   public static void Main() {
      ...
   }
}

Rule 3: When a C# program is launched to run, the execution will start with the first executable statement in the entry point method, and continue to the end of this method. In our first program, there is only one executable statement in the entry point method:

      System.Console.WriteLine("Hello world!"); 

This statement will be executed, when this Hello program is launched to run. Once this statement is done, the program will end.

Please note that the only statement in the main() method calls a pre-defined method named as "WriteLine", which is defined in the Console class in the System namespace provided as part of .NET Framework SDK. This method takes a string of characters as a input parameter, and print this string to the screen. Note that the string of characters must be enclosed in double quote characters.

Exercise: Modify the hello.cs to print the following message to the screen:

Hi There, 

My name is X Y. I am enjoying this book. 

Last update: 2010.

Table of Contents

 About This Book

Introduction of C# (C Sharp)

 What Is C#?

 Installing .NET Framework 4.6.1 SDK

 First Program in C#

C# Program Structure

 Data Type and Variables

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Outdated Tutorials

 References

 PDF Printing Version