Reputation: 12712
In other languages I can set up the method signature like
cookEgg(boolean hardBoiled = true)
This defaults the parameter hardboiled
to true
, if I don't receive a parameter in the method call.
How would I achieve this in C#?
Upvotes: 17
Views: 16092
Reputation: 4966
Default parameters are supported in C# 4 (Visual Studio 2010).
http://msdn.microsoft.com/en-us/library/dd264739(VS.100).aspx
Upvotes: 11
Reputation: 94653
This is not what you look exactly but I think params argument is another answer.
void test(params int []arg) { }
Upvotes: 2
Reputation: 101655
At present, you have to overload the method:
void cookEgg(bool hardBoiled) { ... }
void cookEgg() { cookEgg(true); }
C# 4.0 will add optional arguments - you will be able to write code exactly as in your original sample, and it will work as you'd expect.
Upvotes: 32