7
I have a function defined as follows:
public classeDeRetorno nomeDaFunção(string param1, string param2, string param3 = "")
What does the param3 = ""
? It equals the parameter to ""
?
7
I have a function defined as follows:
public classeDeRetorno nomeDaFunção(string param1, string param2, string param3 = "")
What does the param3 = ""
? It equals the parameter to ""
?
11
Note that this is not equality, is the allocation operator.
This is a parameter with default value (default). If no argument is passed to fill it, this value is used in this variable. It assigns a value to the variable in the absence of the argument that should assign a value to it during the method call.
The most correct name for the resource is optional Arguments, since it allows some arguments not to be passed and the method assumes a value.
Can only be used in the last parameters, can not have a parameter without default value after having one with value default.
It’s an interesting feature because it avoids having to create methods with different signatures to achieve the same goal and/or have to do some algorithm to solve the missing value.
Behold What is the difference between parameter and argument?.
Note that if you pass one null
, the value of the variable will be null, you cannot pass argument to assume this value. It can be seen in:
public static void Main() => WriteLine(teste(null));
public static string teste(string x = "xxx") => x;
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
What a reply full @bigown! Thanks a lot.
Browser other questions tagged c# .net operators method parameters
You are not signed in. Login or sign up in order to post.
No domino c# but seems to me to be a default value if the parameter is not set.
– user28595
then if it comes
null
is equal to that value, otherwise you get the value of the source?– ihavenokia
Possible duplicate of What is the assignment in Action parameter method?
– viana
@Ack Lay, it’s similar, yes, but with that title it didn’t come to me in the suggestions.
– ihavenokia