I have a function defined as follows:
public classeDeRetorno nomeDaFunção(string param1, string param2, string param3 = "")
What does param3 = ""
mean? Does it equal the parameter to ""
?
I have a function defined as follows:
public classeDeRetorno nomeDaFunção(string param1, string param2, string param3 = "")
What does param3 = ""
mean? Does it equal the parameter to ""
?
Note that this is not equality, it is the assignment operator.
This is a parameter with default value ( default ). If no argument is passed to fill it, this value is used in that 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 feature 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 a default value after it has a default value.
This is an interesting feature because you avoid having to create methods with different signatures to achieve the same goal and / or have to do some algorithm to solve the lack of value.
See What is the difference between parameter and argument? .
Note that if you pass null
, the value of the variable will be null, you can not pass argument to assume this value. Can be viewed at:
public static void Main() => WriteLine(teste(null));
public static string teste(string x = "xxx") => x;
See running on .NET Fiddle . Also I put it in Github for future reference .