Thursday, August 11, 2011

C# 4.0 Optional Parameters - Interview Questions

Microsoft introduced Optional Parameters in C# 4.0

it works likes this:

int Add(int a, int b = 5)
{
retrun(a+b);
}

now when you invoke this function like Add(1,2) it will return 3 but when you do Add(1) it will take b's default value as 5 and add 1+5 and return 6.

Optional parameters make interoperating with COM easier. Previously, C# had to pass in every parameter in the method of the COM component, even those that are optional.

No comments:

Post a Comment