With the release of .NET 4.0 C# programmers are now able to created methods with optional parameters like VB programmers doing. Optional arguments are widely used in VBA for long time. Although they make life a little bit easier for programmers (you don’t have to repeat default values in your method calls).
Code Snippet
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace optionalParameters
- {
- class Program
- {
- static void Main(string[] args)
- {
- OptionalParameterTest t = new OptionalParameterTest();
- Console.WriteLine("Optional Parameter : {0}", t.OptionalParameter("One"));
- Console.WriteLine("Optional Parameter : {0} ", t.OptionalParameter("One1", "Two1"));
- Console.WriteLine("Optional Parameter : {0} ", t.OptionalParameter("One1", "Two1","three1"));
- Console.WriteLine("Optional Parameter : {0} ", t.OptionalParameter("One1", "Two1","Three1","Four1"));
- Console.ReadLine();
- }
- }
- public class OptionalParameterTest
- {
- public string OptionalParameter(string one, string two = "Two",string three="Three",string four="Four")
- {
- return one + " " + two + " " + three + " " + four;
- }
- }
- }

Filed under:
C#
Leave a comment