Optional Parameters in C# 4.0

less than 1 minute read

One of the article in Visual Studio Magazine, 5 Traps to Avoid in C# mentions the pitfalls of using “Optional Parameters”

C# 4.0 re-introduced optional parameters concept which was achieved by the pre-C# 4 era using method overloading.

E.g for Optional Parameters using method overloading

    public void GetParameters()
    public void GetParameters(bool flag)
         //Logic goes here

Assume that GetParamters is part of a library the client who consumes the GetParameters() function would be passing false as the default value. If the default value needs to be changed to true for some reason only the library needs to be compiled.

But the default value for a function using Optional Parameters is fed during compile time, hence the client consuming the library needs to be recompiled as well.

More information:

Named and Optional Arguments

Leave a comment