C# 4 new features ~ Basic method invocation changes – Optional and Named parameters

(Note: This blog post was originally published under my old domain(codesmiles.com), here. Web Archive link. (This post is part of my Visual Studio 2010 series) C# 4 brings many new features, couple of them are optional parameters and named parameters. If you have used Visual Basic, you know these features are already available in VB. Adding these to C# makes it even better and helps C# coders who are from visual basic. [more] Optional parameters Optional parameters allows you to create methods with default values for some parameters, so that you can ignore passing values to them. Like.. public string ReadFile(string FileName = “Defaultfile.xml”) { .. } .. string str1; str1 = ReadFile(); //optional parameter ignored string str2; str2 = ReadFile(“Orders.xml”);   You have to specify a value to the optional parameter in function definition as above, this will be used if you don’t supply a value while calling the method. Quick info(Ctrl+I) helps more by showing the optional parameter’s default value. Until C# 3, we had to create unnecessary method overloads if we have to avoid some of the parameters of a method for simplification, instead, now we can specify a default value and code the method appropriately. If