Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use default parameters in C#?

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#?

like image 806
Chin Avatar asked Jul 30 '09 04:07

Chin


1 Answers

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.

like image 148
Pavel Minaev Avatar answered Oct 02 '22 13:10

Pavel Minaev