C# Methods

So far the only method that has been created is the ‘Main’ method, which is required for a C# program to run. It is however possible to create other methods that can be used by this ‘Main’ method. A big advantage of doing this is where the same block of code is used multiple times within a program. By putting this code in a separate method, it can be written once and then called any number of times. Doing this also aids in maintaining the program because if the code needs updating it only needs to be done in one place.

In its most simple form, a method can be defined as follows.

static void MethodName()
{

   // Statement(s) to execute.

}

Calling this method is simply a case of specifying the name, followed by opening and closing parenthesis, together with a semi colon.

MethodName();

Below is an example of a method called ‘Message’, that is called from the ‘Main’ method, which displays the message, ‘Hello world!’ out to the console.

static void Main(string[] args)
{

   Message();

}

static void Message()
{

   Console.WriteLine("Hello world!");

}

When a method is called, it is possible to pass parameters, so that the values can be used within the block of code contained in the method. Here, two variables are declared and initialised in the ‘Main’ method, then their values are passed to the ‘Message’ method for use in the message displayed in the console.

static void Main(string[] args)
{

   string name = "Bob";
   int age = 30;

   Message(name, age);

}

static void Message(string fname, int age)
{

   Console.WriteLine("{0} is {1} years old.", fname, age);

}

When declaring a method that accepts parameters the data type must be included. This must match the data type of the value being passed in. In the above example, the parameters are of ‘string’ and ‘int’ data types respectively. Notice that the names of the variables included in a method call do not have to match the names of the parameters in the method declaration. This is demonstrated here, where the ‘name’ variable is passed in, but the corresponding parameter is called ‘fname’. The variables passed in must however be in the same order as they are in the method declaration. The method name, along with the order and type of parameters form what is called the Method Signature and ensures that the right method is called.

As well as being able to pass values to a method, it is also possible to return values back to the original calling method. This is done using the ‘return’ keyword. The data type of the value being returned must be included in the method declaration, in place of the ‘void’ keyword.

static void Main(string[] args)
{

   string name = "Bob";
   int age = 30;

   Console.WriteLine(Message(name, age));

}

static string Message(string fname, int age)
{

   return fname + " is " + age + " years old.";

}

In the above example, the call to the ‘Message’ method is embedded in the ‘WriteLine’ method output to the console. All the message method does now is construct the message and return it to the ‘Main’ method to be displayed.

Passing by Value and Reference

When passing variables as parameters as shown in the above examples, a copy of the variable value is being passed and not the actual variable. This means that if the parameter values in the method being called are changed, they will not be available to the calling method, even if the names of the parameters match those of the original variables. This is known as ‘Passing by Value’ and is demonstrated here. Note that the ‘fname’ parameter in the ‘Message’ method has been renamed to match the ‘name’ variable.

static void Main(string[] args)
{

   string name = "Bob";
   int age = 30;

   Console.WriteLine(name + " is " + age + " years old.");
   Message(name, age);
   Console.WriteLine(name + " is " + age + " years old.");

}

static void Message(string name, int age)
{

   name = "Fred";
   age = 45;

   Console.WriteLine(name + " is " + age + " years old.");

}

The resulting output is as follows. Notice that the last line incorporates the original variable values.

Bob is 30 years old.
Fred is 45 years old.
Bob is 30 years old.

If it is necessary to allow for the original variables to be updated within a method, then the ‘ref’ keyword must be incorporated into the method call and declaration. This is known as ‘Passing by Reference’.

static void Main(string[] args)
{

   string name = "Bob";
   int age = 30;

   Console.WriteLine(name + " is " + age + " years old.");
   Message(ref name, ref age);
   Console.WriteLine(name + " is " + age + " years old.");

}

static void Message(ref string name, ref int age)
{

   name = "Fred";
   age = 45;

   Console.WriteLine(name + " is " + age + " years old.");

}

As can be seen below, the third line now incorporates the updated values from the ‘Message’ method.

Bob is 30 years old.
Fred is 45 years old.
Fred is 45 years old.

Method Overloading

Method overloading is a feature of C#, along with many other programming languages, that allows for there to be more than one method with the same name, providing the Method Signature is different. As mentioned above, the Method Signature comprises of the method name, along with the order and type of parameters.

Below is an example which contains three methods named ‘Message’. The first version has no parameters, whilst the second has one ‘string’ parameter and finally, the third, which has one ‘string’ and one ‘int’ parameter. Each version returns a string containing a message, that is output to the console, when called from the ‘Main’ method. The second and third versions of the method incorporate the parameters that have been passed.

static void Main(string[] args)
{

   string name = "Bob";
   int age = 30;

   Console.WriteLine(Message());
   Console.WriteLine(Message(name));
   Console.WriteLine(Message(name, age));

}

static string Message()
{

   return "Hello world!";

}

static string Message(string name)
{

   return "Hello " + name + "!";

}

static string Message(string name, int age)
{

   return name + " is " + age + " years old.";

}

The resulting output is as follows.

Hello world!
Hello Bob!
Bob is 30 years old.