The C# 4.0 is the next release of C#. The major theme in C# 4.0 is dynamic programming. Dynamic here means, that objects structure and behavior is not captured by a static type, or at least not one that the compiler knows about when compiling your program.
The secondary theme is co-evaluation with Visual basic. Further any new features will be introduced in both the languages at the same time.
Features of C# 4.0
- Dynamically Typed Objects
- Optional and Named Parameters
- Improved COM Interoperability
- Co and Contra Variance
Optional and Named Parameters
Many a times we come across situations where we need to make call to methods that accepts many arguments, and most of the times not all the arguments that the method accepts will be passed, we usually pass null. Sometimes we create Overloads for better use, but we still have to pass the default values for those Overloads.
For e.g. Consider the following method which creates the table cell.
public TableCellInfo CreateTableCellInfo(string text, System.Drawing.Color textColor, int textSize, double width, double height, Nullable<System.Drawing.Color> backColor, BorderInfo leftBorder, BorderInfo rightBorder, BorderInfo topBorder, BorderInfo bottomBorder, SymbolInfo symbol, bool isTextCenterAligned, TextVerticalValues textVerticalValue)
Not all the arguments are required each time we call the method, the string text, width and height of the cell are mostly used.So we create secondary overload of this method and supply default values in that overload. Finally call the overloaded method.
In C# 4.0 what we can do is we can specify the parameter as optional, to declare a parameter as optional all we have to do is set a default value to that parameter.
So the above method would be modified as shown below
public TableCellInfo CreateTableCellInfo(string text, System.Drawing.Color textColor = System.Drawing.Color.Black, int textSize =8, double width, double height, Nullable<System.Drawing.Color> backColor = System.Drawing.Color.White , BorderInfo leftBorder = null, BorderInfo rightBorder=null, BorderInfo topBorder =null, BorderInfo bottomBorder=null, SymbolInfo symbol=null, bool isTextCenterAligned =true, TextVerticalValues textVerticalValue = null)
And where we need to call the method we can call it by specifying the required parameters only i.e text, width and height.
public TableCellInfo CreateTableCellInfo( Ã¢‚¬Å“DefaulttextÃ¢‚¬, 5, 4);
Incase we want to specify a different value other than the default value for the optional parameters then we can make use of the named parameters and pass the required arguments by names as shown blelow.
public TableCellInfo CreateTableCellInfo( Ã¢‚¬Å“DefaulttextÃ¢‚¬, 5, 4, Nullable<System.Drawing.Color> backColor = System.Drawing.Color.White );
Optional parameters allow you to omit arguments to member invocations, whereas named arguments is a way to provide an argument using the name of the corresponding parameter instead of relying on its position in the parameter list.
Consider this method
public void M(int x, int y = 5, int z = 7);
Y and Z are optional parameters
M(1, 2, 3);//ordinary call of M
M(1, 2); // omitting z Ã¢‚¬€œ equivalent to M(1, 2, 7)
M(1); // omitting both y and z Ã¢‚¬€œ equivalent to M(1, 5, 7)
In case we want to omit Y then we can use named parameter and pass Z by name as shown below.
M(1, z: 3); // passing z by name
If we have overloads of the method, while calling the method if more than one signatures of the method are equally good for calling, the method that doesn’t have optional parameter is preferred.
Consider following example to understand this better:
M(string s, int i = 1);
M(int i, string s = Ã¢‚¬Å“HelloÃ¢‚¬);
If we want to make a function call like
Evaluating the above methods, out of given signature, 2nd (public static void M1(object o) ), 3rd (public static void M1(int i, string s = “Hello”) and 4th method (public static void M1 (int i)) are suitable for call.
Calling a method M1(object o) involves boxing operation and assigning 5 into int is better than converting 5 into object, so that is not a suitable method.
Out of the remaining 2 the method without optional parameter is preferred so the method that is being called is M1(int i) and output is Ã¢‚¬Å“int iÃ¢‚¬.