Named & Optional Parameter, C# 4.0 Part 2
In visual basic we were having optional parameter option which can help you to send only some parameter to methods and reset will take the default values. Same feature is part of C# 4.0 where you can have default values for the parameters.
But one more noticeable feature is “named parameter”. You might have seen in VB also that if you want to set any of the parameter no values then you have to leave all other parameters also which are defined after that variable. To sort this out you can use named parameter which specify the name of the parameter and value for it “seats:4” where seats is the parameter and 4 is the value.
Both of the features are shown below in sample
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ParameterFeatures
{
class Program
{
static void Main(string[] args)
{
Vehicle car = new Vehicle("V6");
Vehicle van = new Vehicle(seats:4);
Console.WriteLine("Engine : " + car.Engine);
Console.WriteLine("Seats : " + van.Seats.ToString());
Console.ReadLine();
}
}
class Vehicle
{
public string Engine { get; set; }
public int Seats { get; set; }
public Vehicle(string engine = "V8", int seats = 5)
{
Engine = engine;
Seats = seats;
}
}
}


[...] Named & Optional Parameter, C# 4.0 Part 2 [...]
Optional Parameter, Named arguments & Default values C# 4.0 « SharePoint - The MOSS
May 6, 2009 at 6:54 am