IMPORTANT PLEASE READ!
Website development and design blog, tutorials and inspiration

C# Program Flow Control and Entry Points

Starting applications and monitor their flow of execution

By , Written on in C#

C# Program Flow Control and Entry Points

763 words, estimated reading time 4 minutes.

In C# applications, the code is executed in a specific sequence. Execution of code starts at the program entry point, usually, the Main method, and from there code is executed sequentially until it gets to the end of the entry point, then the application closes.
 
C# Programming Series
  1. Introduction to Programming
  2. What is C#?
  3. Your First Console Application in C#
  4. Introducing Methods and the Main() Function in C#
  5. Introducing C# Classes and Structs
  6. C# Data Types, Variables and Casting
  7. C# Program Flow Control and Entry Points
  8. Passing Parameters to Methods and Return Values in C#
  9. C# Access Modifiers and Scope
  10. C# Interfaces and Classes
  11. Using Namespaces in C#
  12. C# Conditional Statements
  13. Looping and Iteration in C#
  14. Using Arrays and Lists in C#
  15. C# Constants and Read-Only Variables
  16. Error and Exception Handling in C#
  17. Using Recursion in C#
  18. C# Operator List
  19. Class Inheritance in C#
  20. C# Class and Method Attributes Explained
  21. C# Class Constructors and Destructors
  22. C# Generics Variables
  23. XML Serialization and Deserialization
  24. 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:

  1. class Program
  2. {
  3. static void Main()
  4. {
  5. Console.WriteLine("This is the first instruction executed.");
  6.  
  7. for (int i = 0; i < 5; i++)
  8. {
  9. Console.WriteLine("In a loop...");
  10. //We will do this five times
  11. }
  12.  
  13. int result = myMethod();
  14.  
  15. if (result == 1)
  16. Console.WriteLine("myMethod returned 1");
  17. else if (result == 2)
  18. Console.WriteLine("myMethod returned 2");
  19. else if (result == 3)
  20. Console.WriteLine("myMethod returned 3");
  21. else
  22. Console.WriteLine("myMethod returned something else");
  23.  
  24.  
  25. switch (result)
  26. {
  27. case 1: Console.WriteLine("myMethod returned 1"); break;
  28. case 2: Console.WriteLine("myMethod returned 2"); break;
  29. case 3: Console.WriteLine("myMethod returned 3"); break;
  30. default: Console.WriteLine("myMethod returned something else"); break;
  31. }
  32. }
  33.  
  34. public static int myMethod()
  35. {
  36. return 2;
  37. }
  38. }

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.

Entry Points

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.

  1. namespace ConsoleApplication
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. Console.WriteLine("Main Called");
  8. }
  9. }
  10.  
  11. class Program2
  12. {
  13. static void Main()
  14. {
  15. Console.WriteLine("Main2 Called");
  16. }
  17. }
  18. }

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:

  1. Select the project in Solution Explorer and bring up its properties.
  2. Click on the Application tab.
  3. Make sure that the Enable application framework check box is disabled.
  4. Change the value in the start-up object box to include /main:Program2 where 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.

Last updated on: Friday 23rd June 2017

Did you Like this Post? Why not Like us on Facebook?

 

Comments
  1. dgreatgnazzini
    dgreatgnazzini

    Great samples. Simply stated and well written.

Leave a Reply

Your email address will not be published.