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();
}
}
}