I am using C# .net 3.5 to build an application. I have been working with optional parameter attributes in .net 4.0 with no problems. I did notice that with 3.5 there is the opti
Take a look at the following StackOverflow thread: C# Optional Parameters in .net 3.5
No use in copy pasting everything which has been said there, as the thread covers pretty much everything. Good luck.
The Optional
attribute has been available since C# 1.0, and is used when interoperating with external code, it has no effect on method calls in your own code.
As there is no optional parameters in C# 3, you can use overloading instead:
public static void MethodName(string name, string placeHolder) {
...
}
public static void MethodName(string name) {
MethodName(name, null);
}
(Side note: There is no C# version 3.5, that is a framework version.)
Optional parameters are C# 4.0 language feature so it doesn't matter which framework you are targeting, but you have to compile it using VS 2010 or newer.
Use this syntax in VS 2010 or newer:
public static void MethodName(string name, string placeHolder = null)
{
// body
}
Or this in older one:
public static void MethodName(string name, string placeHolder)
{
// body
}
public static void MethodName(string name)
{
MethodName(name, null);
}