Thursday, June 20, 2013

Named and optional parameter in c#

Named parameter:-

Named parameter in c# is very useful feature in .NET 4.0. It’s very useful because you need not worry about parameter sequence in function call time. Named parameter is nothing special but good to follow during program development.  You can use parameter name during call time . And in below code you can find that I have change sequence of parameter in call time. And it’s working fine.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;

namespace BlogProject
{
    class Test
    {
        public void ShowData(String Name, String Surname)
        {
            Console.WriteLine("Name:- " + Name + " Surname:-" + Surname);
        }
    }

   class Program
    {
        static void Main(string[] args)
         {
             Test t = new Test();
             t.ShowData(Surname: "Kayal", Name: "sourav");
             Console.ReadLine();
         }
    }
}



Optional parameter:-

Optional parameter in C# is very essential feature in c#.  When one parameter is optional in function then we can set default value for this parameter. Now you may think that when optional parameter is needed? Let me give one example(Yes I like it to give ! If you are regular reader then probably you know it)
Think about situation where this parameter value is sometime needed and sometimes not. For example if customer is premium then give rebate of some percentage and if not default rebate is 0%. And think about condition when user will put her particular date time value it will take otherwise default value is today’s date. Talked too much..Let’s see in action.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;

namespace BlogProject
{
    class Test
    {
        public void ShowData(String Name, String Surname="kayal")
        {
            Console.WriteLine("Name:- " + Name + " Surname:-" + Surname);
        }
    }

   class Program
    {
        static void Main(string[] args)
         {
             Test t = new Test();
             t.ShowData(Name: "sourav");
             Console.ReadLine();
         }
    }
}

2 comments:

  1. Got it. Thanks Sourav for clearing my doubts. I was confused why we needed that optional parameter for. You made an example with the "surname" parameter.

    Regards,
    Silvester Norman

    ReplyDelete
  2. Hi,The emphasis now for e-business websites is to improve customer satisfaction and to make the user experience of the site simple, intuitive and efficient. Companies who invest in designing solutions in Web Design Cochin that make life easy for their users are far more likely to achieve customer retention - the key to the success or failure of any business on the web.Thanks........

    ReplyDelete