Posts
201
Comments
1111
Trackbacks
51
March 2010 Entries
C# 4 Named Parameters for Overload Resolution

C# 4 is getting a new feature called named parameters. Although this is a stand-alone feature, it is often used in conjunction with optional parameters. Last week when I was giving a presentation on C# 4, I got a question on a scenario regarding overload resolution that I had not considered before which yielded interesting results. Before I describe the scenario, a little background first.

Named parameters is a well documented feature that works like this: suppose you have a method defined like this:

   1:  void DoWork(int num, string message = "Hello")
   2:  {
   3:      Console.WriteLine("Inside DoWork() - num: {0}, message: {1}", num, message);
   4:  }

This enables you to call the method with any of these:

   1:  DoWork(21);
   2:  DoWork(num: 21);
   3:  DoWork(21, "abc");
   4:  DoWork(num: 21, message: "abc");

and the corresponding results will be:

Inside DoWork() - num: 21, message: Hello
Inside DoWork() - num: 21, message: Hello
Inside DoWork() - num: 21, message: abc
Inside DoWork() - num: 21, message: abc

This is all pretty straight forward and well-documented. What is slightly more interesting is how resolution is handled with method overloads. Suppose we had a second overload for DoWork() that looked like this:

   1:  void DoWork(object num)
   2:  {
   3:      Console.WriteLine("Inside second overload: " + num);
   4:  }

The first rule applied for method overload resolution in this case is that it looks for the most strongly-type match first.  Hence, since the second overload has System.Object as the parameter rather than Int32, this second overload will never be called for any of the 4 method calls above.  But suppose the method overload looked like this:

   1:  void DoWork(int num)
   2:  {
   3:      Console.WriteLine("Inside second overload: " + num);
   4:  }

In this case, both overloads have the first parameter as Int32 so they both fulfill the first rule equally.  In this case the overload with the optional parameters will be ignored if the parameters are not specified. Therefore, the same 4 method calls from above would result in:

Inside second overload: 21
Inside second overload: 21
Inside DoWork() - num: 21, message: abc
Inside DoWork() - num: 21, message: abc

Even all this is pretty well documented. However, we can now consider the very interesting scenario I was presented with. The question was what happens if you change the parameter name in one of the overloads.  For example, what happens if you change the parameter *name* for the second overload like this:

   1:  void DoWork(int num2)
   2:  {
   3:      Console.WriteLine("Inside second overload: " + num2);
   4:  }

In this case, the first 2 method calls will yield *different* results:

   1:  DoWork(21);
   2:  DoWork(num: 21);

results in:

Inside second overload: 21
Inside DoWork() - num: 21, message: Hello

We know the first method call will go to the second overload because of normal method overload resolution rules which ignore the optional parameters.  But for the second call, even though all the same rules apply, the compiler will allow you to specify a named parameter which, in effect, overrides the typical rules and directs the call to the first overload. Keep in mind this would only work if the method overloads had different parameter names for the same types (which in itself is weird). But it is a situation I had not considered before and it is one in which you should be aware of the rules that the C# 4 compiler applies.
Posted On Tuesday, March 16, 2010 12:14 AM | Comments (3)
Community Megaphone Podcast

Last week I had the pleasure of being a guest on the Community Megaphone Podcast with Andrew Duthie and Dane Morgridge. We discussed .NET 4, C# 4, MVC 2, “geek religious wars”, and of course community. You can check out Show #5 here or directly download it.

Thanks to Dane and Andrew for having me on the show!

Posted On Monday, March 15, 2010 3:53 AM | Comments (0)
AIS is hiring!

My company, Applied Information Sciences (AIS), is currently hiring for multiple positions.

AIS is growing and we have immediate needs for all levels of technologists with a focus developing on the Microsoft stack!  We are seeking .NET developers with a strong Object Oriented Foundation around the DC metro area.  Our goal is to find smart people that are technology enthusiasts and interested in staying on the bleeding edge.  If you have a passion for solving complex business problems by building flexible and scalable solutions, then we want to talk to you!  Our clients are demanding the latest technologies and we have upcoming projects using .NET 4.0, ASP.NET MVC, Silverlight 4, VS2010 with SharePoint 2010.  If you have been exposed to these technologies – great!  If not, then we will train you!  Bottom line…we want smart, capable people with a drive for success!

See all of our open positions here.

If you’re interested, feel free to send me a private message through my blog here.

Posted On Thursday, March 4, 2010 1:29 PM | Comments (0)
Philly.NET C# 4 Code Samples

Thanks to everyone who attended my C# 4 presentation last night at Philly.NET.  Both the code and the PowerPoint can be downloaded here:  C# 4 New Features.

Posted On Thursday, March 4, 2010 10:20 AM | Comments (4)

View Steve Michelotti's profile on LinkedIn

profile for Steve Michelotti at Stack Overflow, Q&A for professional and enthusiast programmers




Google My Blog

Tag Cloud