Is there a way in C# to mark a parameter as optional like VB.net's Optional keyword?
There will be in C# 4.0
For now you can either overload your method, set a default value and call the other method. Or set it to nullable.
public void DoSomething(int a)
{
int defaultValue = 1;
DoSomething(a, defaultValue);
}
public void DoSomething(int a, int b)
{
// Do something
}
or
public void DoSomething(int a, int? b)
{
// Check for b.HasValue and do what you need to do
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With