C# Tutorial For Beginners: Working with Methods
Updated: Jan 18, 2022
A method is a block of code used when a specific request calls it. Methods are crucial as they reduce the use of code lines and enhance the program's clarity, debugging, and efficiency.
There are different types of methods that we can use in our program, for example, a function that only runs a code block without receiving or returning a value or a function that can accept parameters and return a value.
Creating your first method
A method is declared with a few mandatory parts that will determine how we can access it and how it can be used in the program.
class Program
{
static void Main(string[] args)
{
MyFirstMethod(); //Call a method
}
private static void MyFirstMethod() //Method
{
//Code to be executed
}
}
Let's review this example by breaking up the method structure:
Private – The security level of the method is configured as private (We can only access it from the same class).
Static – This means that the method is part of the program class and not as an object of the program class (I will cover this topic in future posts).
Void – This means that the method will not need to return any value besides just running its code block.
MyFirstMethod – This is just the method name we must specify to call the method.
() – This is where we specify the different parameters that the method is expected to receive prior. It can execute its code block.
As you can see, we can pass information to methods by just using it as a parameter. Parameters are then used as variables as part of the code execution of the method.
Here are two simple examples that can demonstrate it:
Example 1:
class Program
{
static void Main(string[] args)
{
NumberMultiplication(100,115);//Call Method #1
NumberMultiplication(44,1150); //Call Method #2
NumberMultiplication(13, 15); //Call Method #3
}
private static void NumberMultiplication(int firstNumaber, int secondNumber)
{
Console.WriteLine("The Multiplication result of these numbers is {0}", firstNumaber*secondNumber);
}
}
Result:

Working with Method variables
As I mentioned above, we can use methods with a set of parameters sent to them (or not) when we trigger them in the code. It is now essential to understand how those parameters are affected before and after the block code is executed.
Example 1: No impact on the original variable
class Program
{
static void Main(string[] args)
{
int v1 = 10;
Example1(v1);
Console.WriteLine("Value after function manipulation: " + v1);
}
private static void Example1(int a)
{
a = 90;
Console.WriteLine("Value in function : " + a);
}
}
Result:

Example 2: Send parameters by ‘Reference’
In this example, the original variable will change based on the result of the code executed as part of the method.
class Program
{
static void Main(string[] args)
{
int v1 = 10;
Example1(ref v1);
Console.WriteLine("Value after function manipulation: " + v1);
}
private static void Example1(ref int a)
{
a = 90;
Console.WriteLine("Value in function : " + a);
}
}
}
Result:

Example 3: Send parameters by ‘outer’
When using the 'Outer' keyword, we will send variables to a method without initializing them first. Therefore you must initialize the parameter within the function body before using it.
class Program
{
static void Main(string[] args)
{
int v1;
Example1(out v1);
Console.WriteLine("Value after function manipulation: " + v1);
}
private static void Example1(out int a)
{
a = 1000;
Console.WriteLine("Value in function : " + a);
}
}
Result:

Return Values
I used the ‘void’ keyword in all the examples above, which indicated that the method would not return any value (besides just executing its code block). If you want the method to return a value, you can simply define it as part of the method definition.
class Program
{
static void Main(string[] args)
{
int result =ReturnANumber(10,50,10);
Console.WriteLine(result);//return result of 70