Web Design that taps into the haromny and vision of your dreams.

C# Program Flow Control and Entry Points

Starting applications and monitor their flow of execution

Written By on in Coding

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:

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.

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.

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:

  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




Great samples. Simply stated and well written.

Reply to dgreatgnazzini


Leave a Reply

Your email address will not be published.

If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.


Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me