C# 4 - Default Parameters

Coming from a C++ background, I’m really pleased that there’ll eventually be default parameters built into the next version of C#. But I do think that C++ had a much cleaner way of handling them: it would only allow defaults from right to left, for instance:

void foo(int a, int b, int c=3, int d=4)

It wouldn’t let you do:

void foo(int a=1, int b=2, int c, int d)

C# will, so for the example above, you’d end up with method calls that could look like:

foo( ,  , 3, 4)

One feature that I still really miss from C++ is const. This keyword could be applied to methods or parameters, so a method declared as:

void foo(int a, int b) const
{
}

would be guaranteed to not modify the state of the object (except for mutable members)

A method declared as:

void foo (const Person person)
{
}

would be guaranteed to not modify the person.

Maybe C# 5?

🙏🙏🙏

Since you've made it this far, sharing this article on your favorite social media network would be highly appreciated 💖! For feedback, please ping me on Twitter.

Leave a comment

Comments are moderated, so there may be a short delays before you see it.

Published