C# Methods (Continued, Optional Parameters and Overloading)

[This tutorial continues from a previous tutorial, C# Methods ]

Optional Parameters

In methods you can have optional parameters. To specify an optional parameter you use the equal to sign and give it a default value. All optional parameters must go at the end.

Sponsored Links

Change the VAT method so it looks like this:

static double VAT(double productCost, double currentRate = 20)
 {
  double cR = (currentRate + 100) / 100;
  return productCost * cR;
 }

This time, the currentRate parameter has a default value of 20.

Change the code in static void main to look like this:

static void Main(string[] args)
{
  Console.WriteLine("Enter product cost: ");
  string pC = Console.ReadLine();

  /*Console.WriteLine("Now enter current VAT rate: ");
  string cR = Console.ReadLine();*/

  var result = VAT(Convert.ToDouble(pC));
  Console.WriteLine("Total cost is {0:C}", result);
  Console.ReadLine();

}

This time you do not need to specify the vatRate as it has a default value of 20. When you type VAT( you will see a tool tip and in square brackets it will show you the VAT Rate argument (the square brackets indicate that it is optional).

Arguments Or Parameters?

To clarify: parameters are the names you pass when you make the methods. So in the VAT method, double ProductCost and double currentRate are the parameters, and the arguments are the values you specify when you call that method. They do the same thing, more or less.

Overloading Methods

Overloading methods can become extremely useful.  This occurs when two methods have the same name but pass different parameters. For example, the Console.WriteLine( ) is overloaded and gives you 19 options to choose from.

In the next example we create  two methods, both with the same name: Games. One will be an array data type and the other one will be a string.

Method 1 - Array

static void Games(string[] newGames)
{
 foreach (var g in newGames)
 {
  Console.WriteLine(g);
 }
}

Method 2 - String

static void Games(string newGames)
{        
 Console.WriteLine(newGames);
}

In this example the first method has a parameter called newGames which is an array, and the second has the same parameter but is a string. Calling this method in static void main VS will give you two options to choose from, as seen in the image below.

C# Overloading Methods

Let’s call the first method. In static void main, type:

Games(new[] { "GTA 5", "BF4", "Fifa", "Uncharted" });
Console.ReadLine();

You will see the list of games print out.

When overloading methods, you need to be careful not to give the same parameters, as this will not work.

static void Games(string[] newGames)
        {
            foreach (var g in newGames)
            {               
             Console.WriteLine(g);
            }
        }


        static void Games(string[] newGames)
        {          
        Console.WriteLine(newGames);
        }

You will get a compile time error saying “The call is ambiguous between the following methods or properties.” Since both methods have the same name and same parameters, which method do you run? The compiler does not know, so it flags an error.

Description

In the Console.WriteLine ( ) method there is a description available for the method and each parameter, and you can provide a description for your methods. In VS 2010/2012 just above the Games method, put:

 /// <summary>
 /// Prints Out a list Of Games
 /// </summary>
 /// <param name="newGames">Specifiy an array of games</param>

        
static void Games(string[] newGames)
{      
  foreach (var g in newGames)
 {
    Console.WriteLine(g);
 }

}

As you can see it is an XML comment; when you call the method you should see this:

Tip: You can insert the first 3 forward slashes and VS will do the rest!

C# Method Comments

Summary

This tutorial outlined methods and how to create your own methods. Remember that methods which do not have a return type must have the void keyword. Variables in methods have a local scope and can only be used within that method, and you can call a method by its name.

  • Optional parameters must go at the end
  • To overload a method you give it the same name but different parameters
  • To provide a method description just type three forward slashes