default parameter

  • Thread starter Thread starter news.microsoft.com
  • Start date Start date
N

news.microsoft.com

I used to assign a default value to a parameter in C++. How do you do in
C#?

When I try, it gives an error, "default parameter specifier is not allowed
...."

Thanks.
 
Default values are not supported in C#. You can overload the method,
however.

public Object MyMethod(int number, string word) {
// Do something
}

public Object MyMethod(int number) {
return MyMethod(number, "defaultValue");
}
 
You can not assign a default value to a parameter in c# - it's not allowed.

Other ways you can get similar effects are:
+ Overriden functions (multiple functions with different parameters that
have the same name)
+ Parameter Arrays (allow an unlimited number of parameters that will be
passed back to one function)
+ Nullable parameters (allow null for a parameter to treat it as
'unspecified')
+ Use VB.NET
 
Back
Top