Share via


Does C# have default parameters?

Update:

Named and optional (default) parameters are available starting from C# 4.0. For more information, see Named and Optional Arguments (C# Programming Guide) .

 

In languages such as C++, a default value can be included as part of the method declaration:

void Process(Employee employee, bool bonus = false)

This method can be called either with:

a.Process(employee, true);

or

a.Process(employee);

in the second case, the parameter bonus is set to false.

C# doesn't have this feature.

One reason we don't have this feature is related to a specific implementation of the feature. In the C++ world, when the user writes:

a.Process(employee);

the compiler generates

a.process(employee, false);

In other words, the compiler takes the default value that is specified in the method prototype and puts it into the method call - it's just as if the user wrote 'false' as the second parameter. There's no way to change that default value without forcing the user of the class to recompile, which is unfortunate.

The overloading model works better in this respect. The framework author just defines two separate methods, and the single-parameter one calls the two-parameter method. This keeps the default value in the framework, where it can be modified if necessary.

It would be possible for a compiler to take something like the C++ definition and produce the overloads, but there are a few issues with that approach.

The first one is that the correlation between the code that the user writes and the code the compiler generates is less obvious. We generally try to limit magic when possible, as it makes it harder for programmers. The second issue has to do with things like XML doc comments and intellisense. The compiler would have to have special rules for how it generates doc comments for the overloaded methods, and intellisense would need to have smarts to collapse the overloaded methods into a single method.

Writing overloads yourself is a bit less convenient, but we think it's an acceptable solution.

[Author: Eric Gunnerson]

Comments

  • Anonymous
    March 07, 2004
    I understand why it makes sense for C# not to allow you to define a function with default parameters. However, these justifications don't really hold up for calling a function that's defined in a different language.

    For example, the Microsoft Office COM automation model is filled with default parameters. (Some functions have as many as 30 default parameters -- maybe it's a bad interface design, but we're stuck with it for the time being.) Because C# doesn't support these default parameters, we have to fill in "Type.Missing" for each parameter, which reduces readability/maintainability. To be blunt, this is a real PITA -- it makes C# less suitable than VB.Net for serious Office automation interop.

    It would be nice if C# could take a middle ground here -- not allow you to define functions with default parameters, but allow you to call functions in other languages using default parameters.

  • Anonymous
    March 07, 2004
    The comment has been removed

  • Anonymous
    March 07, 2004
    The comment has been removed

  • Anonymous
    March 08, 2004
    The comment has been removed

  • Anonymous
    March 08, 2004
    The comment has been removed

  • Anonymous
    March 09, 2004
    If there are issues with compiler-inserted default values when calling the method, then why not have the compiler generate the overloaded stub methods?

  • Anonymous
    March 09, 2004
    Well I think it's a bit mysterious when one (VB.NET) of the first two OO .NET languages supports default arguments and the other (c#) doesn't.

  • Anonymous
    March 09, 2004
    The comment has been removed

  • Anonymous
    March 09, 2004
    The comment has been removed

  • Anonymous
    June 08, 2004
    Hogwash.
    Eliminating the ability for the framework writer.
    You are making it tedious for us to write them.
    If a programmer is too lazy to look up in the
    docs what the parameters are, he doesn't
    understand how the purpose, requirements and
    side-effects of the method.
    But your arguement states that it would be
    too difficult for the /doc portion of the
    compiler to handle default parameters.
    Who cares .... just pick out the defaults
    from the method signature and add them to
    the generated docs. I mean you have
    reflection by goodness. Iterate through the
    similiar method names.

    Don't sacrifice the convience of default
    method parameters for the inability or
    lack of intestinal fortitude to tackle
    the /doc feature robustly.

  • Anonymous
    July 25, 2004
    Overloaded Functions don't serve the purpose all the times.
    Assuming i want something like
    function A(string a)
    {
    dosomething
    }

    [fictional function declaration]
    function A(string a,bool b=false)
    {
    dosomething
    if(b)
    dox
    else
    doy
    }

    I can't have overloaded function in this scenario
    function A(string a) {}

    function A(string a)
    { assume default false }

    function A(string a,bool b)

    Ofcourse some would argue that I can give different function name for the last 2 declarations but this approach is not very user friendly

  • Anonymous
    June 28, 2007
    這是我長久的疑問,也是長久以來C#被罵到臭頭之處,因為C 、VB6、VB.NET,就連T-SQL都有default value,為什麼C#沒有呢?我翻譯了Eric Gunnerson的博客,Eric是C# team的重要成員,我想某種程度可以解釋為什麼C#到了2.0還是不願意提供default value的原因。

  • Anonymous
    February 26, 2008
    The comment has been removed

  • Anonymous
    August 18, 2008
    PingBack from http://will.hughesfamily.net.au/20080819/pains-of-moving-from-vbnet-to-c-optional-method-parameters/

  • Anonymous
    August 28, 2008
    PingBack from http://blog.dragonsoft.us/2008/08/28/delphi-methods-and-default-values-convenience-or-contract-enforcement/

  • Anonymous
    October 30, 2008
    Comentaba mi buen amigo Luis en respuesta a un post anterior que, a pesar de todo, no le gusta la idea

  • Anonymous
    October 30, 2008
    Comentaba mi buen amigo Luis en respuesta a un post anterior que, a pesar de todo, no le gusta la idea

  • Anonymous
    November 10, 2008
    After several failed tries, I finally managed to configure my home PC so that it runs the Server 2008

  • Anonymous
    November 10, 2008
    After several failed tries, I finally managed to configure my home PC so that it runs the Server 2008

  • Anonymous
    November 20, 2008
    PingBack from http://forum.codecall.net/c-programming/12017-default-parameters.html#post100093

  • Anonymous
    January 11, 2009
    PingBack from http://sankalpshere.wordpress.com/2009/01/11/a-revelation/

  • Anonymous
    January 20, 2009
    PingBack from http://www.hilpers.com/274108-gibt-es-in-c-optionale/2

  • Anonymous
    January 20, 2009
    PingBack from http://www.hilpers-esp.com/446038-asignacion-de-valores-a-parametros

  • Anonymous
    June 17, 2009
    PingBack from http://thebasketballhoop.info/story.php?id=2690

  • Anonymous
    November 01, 2009
    I need the ability to expose optional parameters to COM calls to guarantee full backward compatibility with legacy code, but if I can obtain the same that in any other way, including annotations I wouldn't mind doing so.

  • Anonymous
    November 15, 2009
    The comment has been removed

  • Anonymous
    November 18, 2009
    Named and optional (default) parameters are available starting from C# 4.0. For more information, see http://msdn.microsoft.com/en-us/library/dd264739(VS.100).aspx

  • Anonymous
    December 08, 2009
    I have to agree with DeveloperChris and others, this lack of default parameters is a PITA and really makes no sense. The arguments given as to why are nebulous at best. If the compiler is having difficulties, then the problem really lies in the compiler and lazy compiler developers, C/C++ compiler developers have figured it out and it works fine, obviously the VB compiler developers figured it out as well. So that argument really holds no water (I think Java compilers allow defaults as well). "The second issue has to do with things like XML doc comments and intellisense. The compiler would have to have special rules for how it generates doc comments for the overloaded methods, and intellisense would need to have smarts to collapse the overloaded methods into a single method." First, isn't IntelliSense SUPPOSED to have the smarts to figure this stuff out? Isn't that the point of the tool? Second, I for one don't particularly care if the compiler has issues generating the doc comments, I prefer to do that myself, because often times its helpful a year down the road to know WHY I did something the way I did. But back again to the first argument, judging by the comments to the article, they're not making it easier on us developers, they are, in fact, making it more difficult.

  • Anonymous
    February 18, 2010

Sachin Bhatt

it will cause an ambiguous situation I guess. Default Parameter is good that we don't have to write the function again. But function over loading works fine but still boring.

  • Anonymous
    December 08, 2010
    The comment has been removed

  • Anonymous
    December 13, 2010
    Using C# is like putting all your eggs into one basket. Once the VS team of MS does not perform well, your company will be in a disadvantageous position in the speed competition. The bitter fact is the VS team often perform really bad.  

  • Anonymous
    December 29, 2010
    Ok so what is a big deal here.Overloading can do most of the job you need.If not, you can use VB.NET create and compile a dll which you can later use in C#(they all go to MSIL).As for C# 4.0 and optional parameters , ok good for us! Remember the old days we didn't have all comfort of today's modern programming languages and IDE.All we need is just a little patience and creative ideas.And all we need is LOVE of course.

  • Anonymous
    March 15, 2011
    You say, " There's no way to change that default value without forcing the user of the class to recompile, which is unfortunate." But your solution (overloading) duplicates code which has to be maintained separately - hope your junior developer remembers to fix the bug in every overloaded method.  But they will be right next to eachother in the source file so he will see them right? The default arg solution allows for there to only be one method to be maintained.

  • Anonymous
    September 11, 2013
    C# FAQ maintainers: this article is out of date since C# 4.0 shipped; consider updating it. This page is easily found when searching for articles on default parameters in C#, and will mislead customers who do not notice that it was published in 2004.

  • Anonymous
    October 15, 2013
    I realize this is from 9 years ago, but C# does indeed have optional parameters now. (not sure when they were added).  Only commenting because this page was in top 10 for google search of "C# default parameters", and this info is outdated and should be edited / removed.

  • Anonymous
    December 20, 2013
    [Author: Eric Gunnerson] wrote :"It would be possible for a compiler to take something like the C++ definition and produce the overloads, but there are a few issues with that approach." These issues are all issues for the compiler-writer or intelli-sense-writer, or people who look at the resulting code. Once you have implemented it, your in a better world i think. I dont think that its magic: If you use it, you should have an idea what it does. Actually i assumed c++ default parameters worked like that. I really would like to see this feature in the future