Optional Parameters in C# 4.0
Apr.21, 2011 in
C#
Just like other languages such as vb, and c/c++, C# can support the optional parameters from 4.0.
here is a code snippet
1: class Program
2: {
3: static void Main(string[] args)
4: {
5: MyTest("P1", "P2");
6: MyTest("P1");
7: }
8:
9: static void MyTest(string parameter1, string parameter2="Default Value 2")
10: {
11: Console.WriteLine("Para1={0}, Para2={1}", parameter1, parameter2);
12: }
13: }
And the returning result will be
Para1=P1, Para2=P2
Para1=P1, Para2=Default Value 2
and the parameter2 will use the default value in the definition if you do not give a value. Quite convenience, isn’t it?

Leave a Reply