Wednesday, July 04, 2012

Named and Optional Parameters

Named and optional parameters have been there for Visual Basic for sometime but haven't been available in C# until the version 4 release. Name parameters allow you to pass parameters by the parameter names disregarding the order of parameters in the method signature. Optional parameters allow you to provide default values for some of the parameters in your method.


Named Parameters
















Optional Parameters

Optional parameters allow you to provide default values for some of the parameters in your method and acts as a type of overloading. If you want to add one or more new parameter to one of your existing methods and you still need the existing functionality to behave as it is and the new parameters doesn't effect the existing functionality, the solutions is to repeat the same method with the additional parameters and a different method body. This is called overloading. 

If you use optional parameters for this purpose you can add the extra parameters as default parameters and handle those in the existing method body. You don't have to add a new method or modify the code where the existing method was called.



No comments:

Post a Comment