Skip to main content

Optional Parameters in C#

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 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

Popular posts from this blog

The view or its master was not found or no view engine supports the searched locations!!!

On an MVC application, if you are getting error " The view or its master was not found or no view engine supports the searched locations " and you see that this view (.cshtml file)is already present under the correct folder structure and every thing is correct and you have also tried a lot of posts on SO on how to fix that and that hasn't helped,  just check the build action for this file and make sure that it is set to "content". I have seen the build action set to None (by mistake)and after you deploy/publish  the application, that view would be missing  and you get this error .

Parallel Programming

If you are writing multi threaded application with .Net , there are various options to choose from. There is Thread,Background worker, delegate etc.  .Net framework 4.5 has introduced another approach towards parallel programming " a sync and await" .  With async n await, code follows a logical structure which looks similar to synchronous code and provides benefits of asynchronous programming. Above is an example of a program which downloads the historical prices from Yahoo finance and saves it as a csv file.  Here Act_Async is an asynchronous method which makes a call to GetStreamAsync and after that performs some operations  (which are some print operation for ex.) . Once those operation are complete and it requires the output of  GetStreamAsync , it await on the return object (which is stream in this case). Once await is called, control is returned to the calling code (which is main method). In Ma...

FSharp -Top tweets (10/09/2013)

(12) Posted: first steps with http://t.co/WvWRwS72oG support vector machine in #fsharp. http://t.co/s9yEA3HZFD (7) If you missed @visemet presenting the #mongodb provider for #fsharp, recording is online via @c4fsharp: https://t.co/9RKx… (6) Stanford Word Segmenter is available on NuGet for #Fsharp and #Csharp http://t.co/M8ZEKQHihq #nlp #dotNet @stanfordnlp (5) Forward pipe from #fsharp and #ocaml in #scala https://t.co/cvZkMbddGF (5) Playing with an upcoming open-source #fsharp #dataframe library and Titanic data set :-) cc @brandewinder @ptrelford http://t.co/pJfUdUG5eu (4) A few spots available for Machine Learning Hands On http://t.co/INgp7OmGki with @moloneymb tomorrow at #skillsmatter #fsharp (2) Looking for tutorials around the line of Learn F# by building ... http://t.co/xcefPaBY9x #fsharp (2) F# Weekly #36 2013 http://t.co/oSK4QX2WBx #fsharp (0) @kot_2010 @tomaspetricek You are right. Macro implementation of the #fsharp compu...