Methods
Methods in c# let you define a behavior or task which composed of a set of codes that you can execute anywhere in the program. methods can accept arguments which supplement the method’s duty. You define a method inside a class. You cannot define a method inside another method. We will see more about methods of classes in later lessons. When a program detects that you are calling a method, it jumps to its definition somewhere in the code and execute its contents. Method promotes software reusability which allows other programmers to effectively use your code just by knowing the parameters and return values of the method.
There is one method a program should find in order to start a program, and that is the Main method. The Main method serves as the starting point of the program. Without it, the program doesn’t know where to start its execution. Parameters are what a method is expecting to receive. Arguments are the values you pass in the parameters of the method. Sometimes, the terms parameters and arguments are used interchangeably. The basic syntax of the simplest method is as follows.
returnType MethodName()
{
code to execute;
}
Let’s look at an example program that uses a simple method that prints a message in the screen.
using System;
namespace MethodsDemo
{
public class Program
{
static void PrintMessage()
{
Console.WriteLine("Hello World!");
}
public static void Main()
{
PrintMessage();
}
}
}
Example 1 – Your First Method
Hello World!
In lines 7-10, we declared our first method. The position inside the class is not important. For example, you can place it below the Main()method instead. You can call (or invoke) this method inside other methods. Write now, we only have Main() as the other method who can possible call PrintMessage(). You can see that the Main() method is declared as static. For the PrintMessage() method to be used by the Main() method, it needs to be static also.
static simply means that the method can be used without creating an instance of the class. The Main() method always requires that it is declared static because the program will use it immediately without creating an instance of the Program class. We will look closely on the static keyword when we reach the topic of Object Oriented Programming. The Program class is being executed when the program runs so both the method we defined and the Main method should be static. We will learn more about this keyword in later lessons.
Continuing with the method we are creating after the word static is the keyword void which indicates that the method will not return or give back a value. Next lesson will demonstrate you how you can return values from a method and use that returned value for different purposes. We then name our method PrintMessage. Notice that we used Pascal Casing on naming our method. This is a convention in C# and can be ignored, but I do recommend to stick to this convention so that you can easily distinguish a method. Also, when naming methods, use verbs because methods are like actions so naming them GoToBed, OpenDoor, Calculate is a good practice. In cases where it will return a bool data, you can name your method with a question such is IsLeapyear, IsTeenager, DoneEating but refrain from adding a question mark after the names.
The pair of parentheses following the name is required to indicate that it is a method. Currently, it has nothing between the parentheses because it has no parameters which will be introduced in a later lesson. We placed a set of curly braces which wraps the body of the method and inside it, we write the code that we will execute.
Inside our Main() method, we called the method we created in line 14. To call a method, simply write its name followed by parentheses. If the method has parameters, then you must place the arguments between the parentheses in the proper order. This will be demonstrated later. Calling the method will execute the code that the method contains inside its body. The execution will jump from the Main() method to the starting point of the PrintMessage() method. For example, when we called the PrintMessage() method in line 14, the program jumps from line 14 to line 9 where the beginning of the method to be called is located. Since we now have a method defined in our Program class, all the methods of the Program class can call the method we created.