How to use Default Parameter on methods.

Tripati.patro
Posted by Tripati.patro under C# category on | Points: 40 | Views : 1868
Default parameter is supported in .Net 4.0 only

private void UseDefaultValue()
{
DefalutValueMethod("Codes");
DefalutValueMethod("Interviews");
// Access the method without passing any value.
DefalutValueMethod();
}

/// <summary>
/// Method to show a message with a default parameter.
/// Supported in .Net 4.0 only
/// </summary>
/// <param name="sectionType"></param>
private void DefalutValueMethod(string sectionType = "Articles")
{
MessageBox.Show(string.Format("DotNetFunda includes: {0} section.", sectionType);
}

Comments or Responses

Posted by: T.saravanan on: 2/22/2011 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi B.Tripati Patro,

its really Nice one...
Sorry for the disturbance...
I am working in 3.5 framework.So i am not check my doubt in this framework.
If i am using Method Overloading concept in your above code means which method access if i call DefalutValueMethod() ...
For example...
private void UseDefaultValue()

{
DefalutValueMethod("Codes");
DefalutValueMethod();
}
private void DefalutValueMethod()
{
MessageBox.Show("No Parameter");
}
private void DefalutValueMethod(string sectionType = "Articles")
{
MessageBox.Show(string.Format("DotNetFunda includes: {0} section.", sectionType);
}

Kindly check it out and inform to me because i am using 3.5 framework.



Posted by: Tripati.patro on: 2/27/2011 Level:Starter | Status: [Member] | Points: 10
Hi Saravanan,

Sorry for late response.

In the above code mentioned by you, if you call DefalutValueMethod(); then the overloaded parameter less method will be invoked not the method having default parameter.

Hope this answers your query.

Posted by: T.saravanan on: 3/7/2011 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi B.Tripati Patro,

Sorry for the delay....

Thanks for your response.

Login to post response