Powerd by dasBlog RSS 2.0
 Tuesday, September 04, 2007

?? Operator

Scott's most recent post on LINQ, showed me that I still have alot to learn about C#. The ?? operator was casually thrown into the mix as if it were as common as +, =, or a semi-colon (;).

It took a couple of searches on Google to find what I wanted, but I finally found a page on MSDN that lists all of the C# operators in 2.0, and the one I was looking for.

The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.

 

int? x = null;

 

//This will assign the value of -1 to y

int y = x ?? -1;

 

//Change x to an integer, and it will be assigned to y

x = 5;

y = x ?? -1; //y now = 5

 

Extension Methods in .Net 3.5

Another tidbit I ran across, was the introduction of extension methods in C# 3.5. Basically, extension methods allow you to create a method that is associated with a type, such that you can call it (and have intelli-sense support), as if it was defined as a method if the type's definition. For example, the Subtract() method of the DateTime type, is defined in the type. With extensions, you can add some code to define a method foo(), such that you can do something like DateTime.foo(); Extension methods are most closely compared to similar functionality in Ruby.

I found an example on Phil Haack's blog which I tried out myself, and reproduced below (note to self, learn how to format code examples nicely).

namespace ExtensionMethods
{
 public static class Extenders
 {
  public static DateTime Ago(this TimeSpan value)
  {
   return DateTime.Now.Subtract(value);
  }

  public static TimeSpan Minutes(this int value)
  {
   return new TimeSpan(0, value, 0);
  }
 }

 class Program
 {
  static void Main(string[] args)
  {
   Console.WriteLine(20.Minutes().Ago()); //With Extensions
   Console.WriteLine(DateTime.Now.Subtract(new TimeSpan(0, 20, 0))); //Without Extensions
   Console.ReadLine();
  }
 }

}

I got intelli-sense on after typing Minutes(), but no intelli-sense on 20. I was also curious to scope, and where extension methods will need to be placed, and how to access them. Moving the Extenders class outside of the main namespace, I got compile a compile time error stating that int doesn't have a Minutes method (standard 2.0 error), as well as text indicating that there were no extension methods (new for 3.5 obviously). Adding a using statement allows the code to compile. I then tried duplicating my extension namespace and adding a 2nd using statement, and got the expected compile error about ambiguous methods. To me, I can see trouble in the future with conflicting Extension Methods in different namespaces. It doesn't look like you can use a fully qualified name either.

This MSDN article gives some technical info on Extension Methods (it's an article containing info on several new language features). Here is some of what I got:

  • Extension methods are declared by specifying the keyword this as a modifier on the first parameter of the methods.
  • Extension methods can only be declared in static classes.
  • Instance methods take precedence over extension methods, and extension methods imported in inner namespace declarations take precedence over extension methods imported in outer namespace declarations.

There is some debate going on in the comments section of the blog post where I got the example from. Some people seem to think that while easier to read (debatable itself), it makes for harder to maintain code. Others claim it breaks OO principles.

See Also: ScottGu's post on Extension Methods

Tuesday, September 04, 2007 1:59:37 AM UTC  #    Comments [0] - Trackback
Programming
Comments are closed.
Archive
<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2009
Adam Salvo
Sign In
Statistics
Total Posts: 176
This Year: 0
This Month: 0
This Week: 0
Comments: 10
Themes
All Content © 2009, Adam Salvo
DasBlog theme 'Business' created by Christoph De Baene (delarou)