C# Program Flow Control and Entry Points
- Introduction to Programming
- What is C#?
- Your First Console Application in C#
- Introducing Methods and the Main() Function in C#
- Introducing C# Classes and Structs
- C# Data Types, Variables and Casting
- C# Program Flow Control and Entry Points
- Passing Parameters to Methods and Return Values in C#
- C# Access Modifiers and Scope
- C# Interfaces and Classes
- Using Namespaces in C#
- C# Conditional Statements
- Looping and Iteration in C#
- Using Arrays and Lists in C#
- C# Constants and Read-Only Variables
- Error and Exception Handling in C#
- Using Recursion in C#
- C# Operator List
- Class Inheritance in C#
- C# Class and Method Attributes Explained
- C# Class Constructors and Destructors
- C# Generics Variables
- XML Serialization and Deserialization
- C# String Formatting Examples
The various code blocks affect the flow of execution; you can jump out to a method or skip over sections. The easiest way to see how a program flows is by using the debugger. We won't be doing any debugging, but we will use the step into the tool to have a look at how the compiler executes the code.
Start a new console project and copy the following code:
- class Program
- static void Main()
- Console.WriteLine("This is the first instruction executed.");
- for (int i = 0; i < 5; i++)
- Console.WriteLine("In a loop...");
- //We will do this five times
- int result = myMethod();
- if (result == 1)
- Console.WriteLine("myMethod returned 1");
- else if (result == 2)
- Console.WriteLine("myMethod returned 2");
- else if (result == 3)
- Console.WriteLine("myMethod returned 3");
- Console.WriteLine("myMethod returned something else");
- switch (result)
- case 1: Console.WriteLine("myMethod returned 1"); break;
- case 2: Console.WriteLine("myMethod returned 2"); break;
- case 3: Console.WriteLine("myMethod returned 3"); break;
- default: Console.WriteLine("myMethod returned something else"); break;
- public static int myMethod()
- return 2;
Once the code is in the editor, press F11 to start the debug step into the tool. The console window will flash up and then return to the editor. You will see that the first brace of the Main() method is highlighted in yellow. Press F11 again and it will jump down to the Console.WriteLine line. This is the line that is just about to be executed. Keep pressing F11 and you will see how the program executes the code. When it gets to the for loop, notice how it checks the conditions in the brackets. First, it initializes the variable and then checks the exit condition before going into the loop for the first time. Press the F11 key a few more times and you will see that the value of i gets incremented, then it checks the exit condition again.
After a couple more iterations, the flow will jump down to the line that calls myMethod and then we see the flow jump again to the first brace of the myMethod statement, which returns 2. We then jump back up to the myMethod calling line and carry on from where we left off.
Next, we have some nested if statements. In the previous tutorial about conditional statements, we said that the nested if statement were inefficient. Now we will see the visual proof and compare that to the switch... case statement.
Step through the if statements with the F11 key and see how each one is executed in turn until we find one that is true. Keep on pressing the F11 key and compare the if statements with the switch statement. Notice how only one statement gets looked at and how many times less you press the F11 key.
After the switch statement the flow falls down to the last brace of the main function and then the program will end.
Every .Net assembly must have an entry point called Main(). This is the method that will be called when the assembly is loaded and is the controlling routine. When the entry point method has finished executing, the program will exit.
An assembly can have more than one Main() method, but only one Main can exist in a single class.
- namespace ConsoleApplication
- class Program
- static void Main(string args)
- Console.WriteLine("Main Called");
- class Program2
- static void Main()
- Console.WriteLine("Main2 Called");
To specify which Main should run you need to use the /main compiler option, either on csc.exe or within the IDE. To configure the IDE to compile multiple entry points you need to:
- Select the project in Solution Explorer and bring up its properties.
- Click on the Application tab.
- Make sure that the Enable application framework check box is disabled.
- Change the value in the start-up object box to include
/main:Program2where program2 is the class containing the entry point to be used.
Unless you specify which entry point to use, the code will not compile.
Summary and Conclusions
In this tutorial, we saw how the flow of execution jumps arond the code and follows a set pattern. We also saw that the switch statement is more efficient than multiple nested if statements.
Stepping into code using F11 is one of the most important features of the C# debugger and allows the programmer to analyse each line before it is executed. This is very useful for finding the cause of errors.