A couple of days ago Jan Welker launched a new German .NET related community site, .NET-Forum.de. I didn't expect the developsphere to require just another site, but there are already 51 users registered, even though the site wasn't advertised anywhere except the dotnet-snippets.de newsletter.
Jan set up Community Server 2007.1 to drive the site (Did you know that .NET related non-profit communities may receive a free license?) I try to support Jan whenever he experiences issues with CS or has a configuration question. So for me it's an appreciated opportunity to get to learn CS's forum capabilities (Till now I only used the blogging part.)
I wish Jan success, and maybe this post will lead some more people to his site.
Most you will probably know about Extension Method introduced with C# 3.0. If not, I strongly recommend to read ScottGu's explanation.
Anyway, a couple of days ago Brad Wilson posted an experiment:
What I wasn't sure was whether or not you could call these extension methods when you have a null instance of the object, since they're instance methods. The C++ guy in me said "sure, that should be legal", and the C# guy in me said "it's probably illegal, and that's too bad". Amazingly, the C++ guy in me won!
This code executes perfectly:
using System;
public static class MyExtensions {
public static bool IsNull(this object @object) {
return @object == null;
}
}
public class MainClass {
public static void Main() {
object obj1 = new object();
Console.WriteLine(obj1.IsNull());
object obj2 = null;
Console.WriteLine(obj2.IsNull());
}
}
When you run it, it prints out "False" and "True". Excellent!
When I read that I immediatley thought of another application. I guess all my readers are familiar with String.IsNullOrEmpty which was introduced with .NET 2.0. So I asked myself if you can make IsNullOrEmpty a parameterless extension method:
using System;
public static class MyExtensions
{
public static bool IsNullOrEmpty(this String s)
{
return String.IsNullOrEmpty(s);
}
}
public class MainClass
{
public static void Main()
{
string s1 = "Hello world";
Console.WriteLine(s1.IsNullOrEmpty());
string s2 = null;
Console.WriteLine(s2.IsNullOrEmpty());
}
}
Again, it prints out False and True. And in my opinion this syntactic sugar looks much more elegant than the ordinary String.IsNullOrEmpty(s2).
If only C# would support extension properties...