In this article we’ll discuss Optional Parameters in C#. The concept of ‘Optional Parameters’ was first introduced in C# 4.0. Below we’ll discuss all key points of optional parameters, how and where we can use these parameters.

If we say, ‘Optional Parameters’ or “default arguments“, then both mean the same. As its name indicate these parameters are Optional Parameters and one is not forced to pass these parameters. User can pass compulsory parameters as mandatory and “Optional Parameters” will take its default value.  If you will pass “Optional Parameters” value, then it will consider and use that else default value will be used.

Key points of Optional Parameters:

  • Optional Parameters are applicable for methods and constructors too.
  • If you will not pass the value to ‘Optional Parameters’ then it will consider its default value.
  • Default value to Optional Parameters is defined in its definition.

Below we have shared one code sample in which we have a method named “CustomerInfo” which has 2 mandatory and next two as optional parameters. When we’ll call this method, we have to pass mandatory parameters and rest are optional.

public  class Program
    {
        public class codeconfig
        {
            static public void CustomerInfo(string Firstname,
                                           string Lastname,
                                           string DOB = "01/01/2001",
                                           string BankName = "ABC bank" )
            {
                Console.WriteLine("First Name: {0}", Firstname);
                Console.WriteLine("Last Name: {0}", Lastname);
                Console.WriteLine("BankName: {0}", BankName);
                Console.WriteLine("DOB: {0}", DOB);
                Console.Read();
            }

        }
        static void Main(string[] args)
        {
           codeconfig.CustomerInfo("Rakesh", "kumar");
         }
    }
}

you can observe optional parameters as highlighted below.

Optional Parameters

Optional parameter always come after compulsory parameter, if you will place optional parameters before compulsory parameter then following error will be there on build.

Optional Parameters

Can we have all parameters as optional parameters?

Yes, we can have all parameters as optional and only in this way optional parameter can be the first parameter.

Please see below we have all optional parameters and when we call this function, we can choose either to pass these parameters or not.

Optional Parameters

Below will be the output of above code sample. In first method call we pass names and same names appear in first green box (below) and in next function call we do not pass even any parameter and all default values are printed in this call.

Optional Parameters

In this article we have explained you about Optional Parameters in C#. We hope this has cleared your doubt on Optional Parameters usages in C#. For more information you can visit HERE.

For more information you can visit our C# Section for more such articles.

Leave a Reply

Your email address will not be published. Required fields are marked *

Explore More

Working With File Class In C#

In C#, ‘File’ class provides many static methods for moving, copying, and deleting files. These Static methods involve moving a file and copying and deleting a file also. Let’s check

How to Read a Text File in C#

In C# ‘File’ class provides static methods to read given text file data. The File.ReadAllText() method opens a text file, reads all the text present in the file into a

C# | How to use Interface references

In C#, you’re permitted to make a reference variable of an interface type or in other words, you’re permitted to form an interface reference variable. Such kind of variable can