Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is overloading the only way to have default function arguments in C#?

Tags:

c#

overloading

Is it true that the only way to handle default function arguments is through function overloading?

For example, in PHP I can do this:

function foo($x, $y=0)
{
}

Would the best way to handle it in C# be this?

void foo(int x)
{
  foo(x, 0);
}

void foo(int x, int y)
{
}

Example lifted from here

Edit

Made the C# example into actual C# (Thanks Blair Conrad)

like image 380
Mark Biek Avatar asked Sep 02 '08 17:09

Mark Biek


People also ask

Can C functions have default arguments?

Default arguments are only allowed in the parameter lists of function declarations and lambda-expressions, (since C++11) and are not allowed in the declarations of pointers to functions, references to functions, or in typedef declarations.

Can overloaded functions have default arguments?

No you cannot overload functions on basis of value of the argument being passed, So overloading on the basis of value of default argument is not allowed either. You can only overload functions only on the basis of: Type of arguments. Number of arguments.

Which are the rules for default arguments?

A default argument is a value in the function declaration automatically assigned by the compiler if the calling function does not pass any value to that argument. The values passed in the default arguments are not constant. These values can be overwritten if the value is passed to the function.

How can we pass arguments to functions by default?

Passing arguments by reference ¶ By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function).


2 Answers

Just to satisfy some curiosity:

From Why doesn't C# support default parameters?:

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.

like image 114
Giovanni Galbo Avatar answered Oct 12 '22 22:10

Giovanni Galbo


Yes, that'd be best, except you'd omit the $s on the parameter names, as others have pointed out. For those interested in the rationale behind the lack of default parameter values, see @Giovanni Galbo's explanation.

like image 42
Blair Conrad Avatar answered Oct 12 '22 22:10

Blair Conrad