- Michael Barton |
- C# |
- May 30th, 2010
When writing a method in .NET (C# or VB), you typically have to overload that method if you want to provide default or optional parameters. Now, thanks to C# 4.0 and VB 10, you can use optional parameters.
For example, prior to C# 4.0, you had to do the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 | // Assume we're using a class and the following stuff is from our class // no parameters public void myMethod() { myMethod("optional"); } // one parameter public void myMethod(string str) { // STUB } |
As you can see from the preceding code, you need to type more than one function. To each their own. Some people like doing this, some people don’t. I like how it works in PHP:
1 2 3 | function myFunc($optional="hello world!") { // STUB } |

