简体   繁体   中英

Default Value in a C# Function

is it possible to do this in C#? (in C++ its not)

function sum ( int a = 9, int b = 4){

}

and then call the function like :

int someValue = sum(, 14) // so 14 is for the second value

C# 4 allows named arguments and optional parameters 1 :

int Sum (int a = 9, int b = 4)
{
    return a + b;
}

Then:

Sum(10, 5);       // Positional arguments as "normal"
Sum(b: 5);        // Use the default value for a
Sum(a: 5);        // Use the default value for b
Sum();            // Default both parameters
Sum(b: 1, a: 10); // Arguments can be reordered

EDIT: For overloaded methods, if there are multiple matches the compiler checks whether any of those candidates are only valid due to giving default values. If that's the case, those candidates are effectively given priority. This is one of the tie-breaking rules listed in section 7.5.3.2. There's no preference for just using "fewer" default values; it's an all or nothing approach. I give an example of this in my article on overloading .


1 It's unfortunate that the terminology is often confused, even by Microsoft. Parameters have always have names - what's new is that you can specify the name in the calling code, for the argument. On the other hand, you make the parameter optional by specifying a default.

With C# 4 you can do int someValue = sum (b: 14);

EDIT - as per comments:

C++ can have default values for parameters... these parameters can be "skipped" when calling a method/function BUT the rule in C++ is that only the right-most parameters can be skipped... which makes it possible in C++ to call sum() and sum (7) refering to a but NOT the call you give as an example...

Yes, since .NET 4 Framework this is possible using Optional Arguments

public int sum(int x = 0, int y = 0)
{
     return x + y;
}

Then call it:

int sumvalue = sum(y: 15);

Important:

Optional parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters. Comma-separated gaps in the argument list are not supported. For example, in the following code, instance method ExampleMethod is defined with one required and two optional parameters

Yes this is possible in C# by using named and optional arguments .

public int Sum(int a = 1, int b = 2)
{
    return a + b;
}

Console.WriteLine(Sum(2));    // Writes 4.
Console.WriteLine(Sum());     // Writes 3.
Console.WriteLine(Sum(b: 4)); // Writes 5.

This will result in a being set to 2 and b having it's default value, 2. It will always populate the arguments in order, so if you only pass one argument then only a gets set and the rest will use their default values.

Sum(2)

This will result in a being set to it's default 1 and b being set to it's default 2. As no arguments have been supplied.

Sum()

This will result in a having it's default value 1, and b being set to 4. The reason order of the arguments is ignored in this case is because we're specifying that b should be set to 4 using a named argument.

Sum(b: 4)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM