Monday, April 8, 2013

C# 4.0 - Named and Optional Arguments

C# 4.0 has new feature called Named and Optional Arguments. Named parameters are useful  to provide a way to pass the arguments by name. This is really helpful when the number of parameters are too many.

  Optional parameters are also useful in a way that it can help lot to avoid unnecessary parameters in method call.
 There was a way to pass optional parameters since the early days of C# 1.0. We had param keyword using that we can pass optional parameters. But the limitations of param is the user can not provide multi type  optional parameters .  The another drawback of param is the optional parameters have to be the last parameters only.  
 C# 4.0 addresses all the problems that we have with earlier versions of C#.   It provides easy to use syntax.  
public int AddNumbers(int n1, int n2=10, int n3=30, int n4=40);
To mark the parameters as optional it is compulsory to provide default values for optional arguments.   In above example n2,n3 and n4 are optional parameters.
The above method can be called in following way.
AddNumber(10,20,30,40);  // Providing all the parameters.
AddNumber(100,200,300);  // Not providing the value for n4.
AddNumber(10,20);        // Not providing the value for n3 and n4.
AddNumber(100);  // Not providing the value for n2, n3 and n4.
When value for optional parameters is not provided it will take the default value for that parameter which is defined in method definition.  Here in case of last method AddNumber(100) the value for n2, n3 and n4 will be taken as 20,30 and 40 respectively. 
class TestArguments
        /// <summary>
        /// MEthods having Optional parameteres.
        /// Note : Optioanl arguments are declared by simply providing default valus for paramteres.
        /// </summary>
        /// <param name="n1">First number which is not optional.</param>
        /// <param name="n2">2nd number having default value hence optional</param>
        /// <param name="n3">3nd number having default value hence optional</param>
        /// <param name="n4">4nd number having default value hence optional</param>
        /// <returns></returns>
        public int AddNumbers(int n1, int n2=10, int n3=30, int n4=40)
            return n1 + n2 + n3 + n4;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharp4
    class Program
        static void Main(string[] args)
            TestArguments objTestArg = new TestArguments();
            int result = 0;
            //calling the method with 4th parameter as optional
            result = objTestArg.AddNumbers(10, 100,200);
            //calling the method with 3rd and 4th parameters as optional
            result = objTestArg.AddNumbers(10, 20);
            //calling the method with 2nd, 3rd and 4rd parameters as optional
            result = objTestArg.AddNumbers(10);
You can see in above example the various way to call AddNumber() methods with optional arguments.

Named arguments

C# 4.0 has another cool feature which lets the user to call method by providing named parameters for methods.
C# 4.0 does not allow to skip arguments between  commas such as  in M(1,,3) but we can pass any argument by name instead. 
For  examples : 
    AddNumbers(10, n4: 3); // passing value of n4  by name
    AddNumbers (n1:100, n4:200 n2: 3); //passing n1,n4 & n3 in different order
AddNumbers(n1:100 , n3:200); // passing n2 and n3.
Above all method call parameters are valid. When parameters are not in the same order as it  defined in method signature, arguments are always evaluated in order they are passed.
Optional and named arguments can be also be used with indexers and constructors.

No comments:

Post a Comment

Software Development Blogs - BlogCatalog Blog Directory RSS Search Technology Blogs - Blog Rankings Blog Directory