Share via


optional arguments in C#

<Note: Optional arguments are now available as of C# 2010 />  

One of the things I missed a lot when I moved to C# is optional arguments. In C++ optional arguments are used a lot. Code as below is a common sight.

 void foo(int reqdParam, int optParam = 0){    // ...} foo(5);  // gets compiled as foo(5,0)foo(5, 10);

The reason it is not included in C# is mainly due to versioning problem. Optional arguments are handled in most programming languages by inserting the default value of the optional argument at the call site. So for the above code foo(5) is compiled as foo(5, 0).

The versioning issue comes to play if the call site and the method are in different assemblies. In the next version of the method the default value of optParam may change from 0 to 1 and 0 can become an unsupported value. However the calling code will still contain 0 and hence we may get a run-time issue. The way to get around is re-compiling all the assemblies that contains calls to the method and this simply does not scale.

Another way of handling optional argument would be to automatically generate method overloads based on optional arguments. So the above code on compilation would yield something like

 void foo(int reqdParam){    foo(reqdParam, 0)}void foo(int reqdParam, int optParam){    // ...} foo(5); // calls the first overloadfoo(5, 10); // calls the actual function

This is versioning safe. However this is not used by most languages including C#. I do not know why this is not used. This is versioning safe and at the same time gives all the benefits of optional arguments. Side effects would be code-bloat, inclusion of these methods in the call-stack.

Comments

  • Anonymous
    February 23, 2006
    The comment has been removed

  • Anonymous
    February 23, 2006
    The comment has been removed

  • Anonymous
    February 23, 2006
    The comment has been removed

  • Anonymous
    February 24, 2006
    Maybe it is better to explicitly declare the overloads than to have them automatically generated so that there are no surprises.

    The .NET wrapper for Office automation should have had the overloads declared so that the functional equivalent of optional parameters would be available from C#. (Especially with those long lists of parameters!)  You could always roll your own wrapper, a little reflection goes a long way.

  • Anonymous
    February 24, 2006
    TAG I think you are getting it wrong here. Default values are language only feature and there is no support in the framework to do that. So run time doesn't come into the picture. When I say versioning issues exist in default parameters I mean it exists in languages like C++ which support default value by call-site manipulation. C# doesn't support at all so its not effected by this.

    However, I do feel that C# should've supported default parameters...

  • Anonymous
    February 24, 2006
    In C++ there were no runtime like in .NET
    This make difference.

    The fact that one Microsoft product limit features of another Microsoft product is Microsoft problem ;-)
    Nobody else.

  • Anonymous
    March 06, 2006
    The comment has been removed

  • Anonymous
    March 09, 2006
    can optional arguments will be of referance type .If so what is the default value of that argument.

  • Anonymous
    February 14, 2007
    The comment has been removed

  • Anonymous
    November 27, 2007
    In VB.NET you have optional parameters and they can be specified by name like: x.doSmth("Doe", "John", age:=23, valid:=False) That is of course rare case (for Office Dev it's not easy to NOT have this feature because of "one-million-optional-parameters-per-function" policy). If it's possible in VB.NET why not in C#? I think it's not a technical issue but "ideological" one.

  • Anonymous
    December 04, 2007
    Can someone please tell me how to convert type 'object' to 'System.Windows.Forms.ListViewItem’ explicitly as it can not be don implicitly as far as i know? Thanks in advance

  • Anonymous
    February 07, 2008
    The comment has been removed

  • Anonymous
    February 07, 2008
    Tuesday, December 04, 2007 1:02 PM by Mohd Can someone please tell me how to convert type 'object' to 'System.Windows.Forms.ListViewItem’ explicitly as it can not be don implicitly as far as i know? Thanks in advance


I think your problem will be solved if you use typecasting. for eg.            object objTest = new object();            ListView lv = new ListView();            lv =(ListView) objTest;            lv.Items.Add("Hi");            lv.Items.Add("Is it working.");            lv.Items.Add("Yes/No"); I hope it will work.

  • Anonymous
    May 21, 2008
    Public Sub PopulateUserRoles(Optional ByVal SpecificUser As String = "") Can you translate it to c#.net

  • Anonymous
    August 07, 2008
    [Optional parameters in C#] Abhinaba: You are right to say that the C# language does not have support for optional parameters. However the .Net Framework provides that support through the [Optional] parameter attribute. All you need is to import the System.Runtime.InteropServices namespace and add the [Optional] parameter attribute before the desired parameter. Note that there are some issues with this approach when a C# method that includes the [Optional] parameter attribute is interacting with either a VBScript or a VB.Net code. Tarek

  • Anonymous
    November 14, 2008
    The problem is how to overload constructors. For example: MyObj obj = new MyObj(int a=0, int b=0, int c=0) { this.a = a; this.b = b; this.c = c; ... a lot of stuff ... } Should be implemented as MyObj obj = new MyObj(int a, int b, int c) { ... a lot of stuff ... } MyObj obj = new MyObj(int a, int b) { this.c = 0; ... a lot of stuff ... } MyObj obj = new MyObj(int a) { this.b = 0; this.c = 0; ... a lot of stuff ... } MyObj obj = new MyObj() { this.a = 0 this.b = 0; this.c = 0; ... a lot of stuff ... } I am very lazy writing code, so optional parameters for me is very usefull.

  • Anonymous
    December 23, 2008
    What about omitted parameters? If you overload a function as: public void MyFunction(string x) {...} public void MyFunction(string x, string y) {...} public void MyFunction(string x, string y, string z) {...} It doesn't allow you to do this: MyFunction("a", , "c"); In VB you can accomplish that functionality with this: MyFunction(ByVal x As String, Optional ByVal y As String = "", Optional ByVal z As String) MyFunction("a", , "c") It may still have the flaw of compiling the default value from the object's assembly into the calling assembly.

  • Anonymous
    April 30, 2009
    Sorry, but I have to say this - not having optional function arguments in C# is retarded. Overloading isn't the answer to everything. Of course the compiler generates the additional parameter, but who cares? What a ridiculous "design decision".

  • Anonymous
    December 29, 2009
    The comment has been removed

  • Anonymous
    January 21, 2010
    Now available in VS 2010 - MSDN link: http://msdn.microsoft.com/en-us/library/dd264739(VS.100).aspx