C# 4.0 has a nice feature of creating methods with optional parameters which allows caller to pass only required parameters and not all. This is particular helpful in methods which needs a lot of parameters.
Method with optional parameters :
Out put :15
Here caller is only passing one parameter i.e. 5 and output is 15 because default value for parameter b is used which is 3.
but, care should be taken when you create methods with optional parameters in a library which is used by third party application (where you have less control) because by just changing the default value in the library , if this new library is deployed (with every thing as it is), client apps will NOT see the change. Client application also needs to be re-compiled with the updated library to see the changes because of the new default value.
in the above example , if OptionalParam.exe uses updated library.dll (without recompiling the complete solution ) where default value of b has been changed from 3 to 4, out put will still be 15.
strange !!! here is the reason.
when compiler compiles the solution and it identifies a call to method with optional parameters, and if optional parameters are not passed by caller, compiler passes the default value of those optional parameters to callee. so changing the values of optional params inside the called method won't have any effect since this will be overridden by passed values.
This is IL code for Main method . If you see IL_0006 and IL_0007, compiler is passing arguments to the methods in the library.dll. Here IL_0006:ldc.i4.5 pushes 5 on stack . This parameter has been passed by caller.
IL_0007:ldc.i4.3 pushes 3 onto stack. This is second parameter to the method which is optional and NOT passed in the code. but compiler passes this default values in the absence of any value for optional parameter.
This explains that by just updating the dll and not recompiling the solution which references the dll will not see the effect of any change in the default value.
Method with optional parameters :
Out put :15
Here caller is only passing one parameter i.e. 5 and output is 15 because default value for parameter b is used which is 3.
but, care should be taken when you create methods with optional parameters in a library which is used by third party application (where you have less control) because by just changing the default value in the library , if this new library is deployed (with every thing as it is), client apps will NOT see the change. Client application also needs to be re-compiled with the updated library to see the changes because of the new default value.
in the above example , if OptionalParam.exe uses updated library.dll (without recompiling the complete solution ) where default value of b has been changed from 3 to 4, out put will still be 15.
strange !!! here is the reason.
when compiler compiles the solution and it identifies a call to method with optional parameters, and if optional parameters are not passed by caller, compiler passes the default value of those optional parameters to callee. so changing the values of optional params inside the called method won't have any effect since this will be overridden by passed values.
this is the IL code for library.dll which has a method with optional parameters
This is IL code for Main method . If you see IL_0006 and IL_0007, compiler is passing arguments to the methods in the library.dll. Here IL_0006:ldc.i4.5 pushes 5 on stack . This parameter has been passed by caller.
IL_0007:ldc.i4.3 pushes 3 onto stack. This is second parameter to the method which is optional and NOT passed in the code. but compiler passes this default values in the absence of any value for optional parameter.
This explains that by just updating the dll and not recompiling the solution which references the dll will not see the effect of any change in the default value.
Comments
Post a Comment