Author:
Monday, November 02nd, 2009

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

  1. Dynamically Typed Objects
  2. Optional and Named Parameters
  3. Improved COM Interoperability
  4. 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.

Simple example:

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

Overload resolution

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(object o);
M(int i, string s = €œHello€);
M(int i);

If we want to make a function call like

M(5);

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€.

References:

http://code.msdn.microsoft.com/csharpfuture/Release/ProjectReleases.aspx?ReleaseId=1686

Category: .Net
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses

  1. The post is about C# 4.0 with its feature the post is good & informative thanks for the awesome post as we got useful information through it.

  2. How could I have missed this blog! Its amazing. The design is impeccable, as you know exactly what to do to get people to come to your page! I also like the perspective you brought to this issue.

Leave a Reply


 

WP-SpamFree by Pole Position Marketing