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

Introducing Methods and the Main() Function in C#

The Main Method

By , Written on in C#

Introducing Methods and the Main() Function in C#

825 words, estimated reading time 4 minutes.

This tutorial will introduce methods, where we will code a simple method and call it from another method. We will also look at the Main method of a C# application.
 
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

In the last tutorial, we wrote a simple hello world program where we saw the Main method, but we didn't explain anything about what it is or what it does. The "Main" is a method that must exist in every program, as the compiler uses this to decide what to do when the program runs. This is called the entry point for the program.

In this tutorial, we will look at the Main method and we will also see how to create a new method that we can run (call) from the Main. Let's have a look at our Hello World again.

  1. using System;
  2.  
  3. class HelloWorld
  4. {
  5. static void Main()
  6. {
  7. Console.WriteLine("Hello World!");
  8. }
  9. }

As you can see on line 5, our Main method is declared using static void Main()

Note: The M is capital and is case sensitive. The Main method must always be marked as static otherwise it cannot be run. We will look at static and void in a later tutorial where we look at variable types and methods in more detail. In a nutshell, they describe to the compiler where the method can be called from (static means can be called without an instance of the class) and what the method will return (void, in this case, means nothing is returned).

All Methods are blocks of code, and a block of code is always encased within curly brackets (braces).

Methods are always executed from the top brace down to the bottom brace, in sequential order. When the compiler reaches the end of the Main method, the program exits.

Creating Methods

We can add another method to our project in much the same way as the Main method. After the closing brace of the Main method, we will add in another method called "myMethod". Also, add in an empty code block with open and close braces.

The method name can be anything you like as long as it follows a few general rules. Method names must:

  1. Be Unique
  2. Not the same as a keyword
  3. Contain only letters or numbers or the underscore character (_)
  4. must not start with a number

There are also several guidelines that should be observed for good coding practice, but they do not affect the compiler or the running of the program.

Going back to our old Hello World program, the code should look like this when you add our new method.

  1. using System;
  2.  
  3. class HelloWorld
  4. {
  5. static void Main()
  6. {
  7. Console.WriteLine("Hello World!");
  8. }
  9. static void myMethod()
  10. {
  11. }
  12. }

Now we need our method to do something, so add another line between the braces

  1. Console.WriteLine("myMethod Has Been Called");

Try and build your project now, using Ctrl+Shift+B and hopefully you will receive a successfully built message. Now run the project using the F5 key, or the play symbol on the toolbar.

Can't see the console window?

Depending on the configuration of your installation of Visual Studio, you may see a console window flash up and vanish very quickly. If this is the case we need to stop the program from exiting by adding another line into the Main method. After the Console.WriteLine line, add a new line

  1. Console.ReadLine();

This will cause the program to wait for an input before exiting. Alternatively, you can run the program without debugging (Ctrl+F5), which will cause the program to output a message "Press any key to continue..." and wait before exiting. The downside of this method is you cannot debug your program.

Run the program now, and you should see the console window with a line that says "Hello World!".

Hello World Output
Hello World Output

Why does it not show "myMethod Has Been Called" as well?

The simple answer is that we have not "called" myMethod yet, so the code in myMethod does not get executed. Press the Enter key to close the program and return to the editor. We can fix our little problem by adding myMethod(); between the Console.WriteLine and Console.ReadLine lines. This will call myMethod and cause the compiler to break out of Main and into myMethod. When it reaches the end of myMethod, it will return to the point in Main where it left off. This is explained in more detail in a later tutorial about Flow Control.

Now when you run the program, you will see two lines on the console window.

Hello World Output
Hello World Output

Completed Source Listing

  1. using System;
  2.  
  3. class HelloWorld
  4. {
  5. static void Main()
  6. {
  7. Console.WriteLine("Hello World!");
  8. myMethod();
  9. Console.ReadLine();
  10. }
  11.  
  12. static void myMethod()
  13. {
  14. Console.WriteLine("myMethod Called");
  15. }
  16. }

Summary and Conclusion

In this tutorial, we have seen the Main method and how it works, we covered code blocks using braces and creating a simple method as well as calling it from Main. We also touched on program flow control.

In the next tutorial we will have a closer look at methods, how to pass parameters and return a result from a method.

Last updated on: Saturday 24th June 2017

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

 

Comments

There are no comments for this post. Be the first!

Leave a Reply

Your email address will not be published.