tags:

views:

277

answers:

3

I was looking through the "Domain Oriented N-Layered .NET 4.0 Sample App" project and ran across some code that I do not understand. In this project they often use syntax like the following to check arguments for null:

public GenericRepository(IQueryableContext context,ITraceManager traceManager)
{
    if (context == (IQueryableContext)null)
            throw new ArgumentNullException("context", Resources.Messages.exception_ContainerCannotBeNull);

Why would you cast null to the type of the object you are checking for null?

+6  A: 

I wouldn't do a cast. There's no reason for it in this case.

John Saunders
That's what I thought. It's just so odd since its a Microsoft sponsored open source project to provide best practices, and there seems to be no point to it.
Jace Rhea
+3  A: 

There is no reason to cast null in the given example. It might be for legibility... I don't know, I wouldn't do this =P

In some cases [which doesn't include the case covered in this topic] you have to cast to INullable before you can check to see if a variable is null. Otherwise, you have to use object==default(TypeOfObject)...

ItzWarty
+6  A: 

It's pointless in the example given.

While not applicable in this case, there is sometimes a need to cast null (or at least there was before default(T) was added. Consider the following:

void DoSomething(string x) {
    ...
}

void DoSomething(object x) {
    ...
}

DoSomething(null);            // compiler can't infer the type
DoSomething((string)null);    // string type is now explicit
DoSomething(default(string)); // same as previous

EDIT

Just thought of another case where you would have to do the cast when testing equality. If you had an object that had an overloaded == operator that allowed comparison with two reference types, comparing against null would be ambiguous. However because IQueryableContext is most likely an interface and interfaces cannot overload the == operator, I still don't see any valid reason to do it in the example you gave.

class CustomObject {

    private string _id;

    public CustomObject(string id) {
        _id=id;
    }

    public static bool operator ==(CustomObject lhs, CustomObject rhs) {
        if (ReferenceEquals(lhs, rhs)) { return true; }
        if (ReferenceEquals(lhs, null)) { return false; }
        if (ReferenceEquals(rhs, null)) { return false; }
        return lhs._id == rhs._id;
    }

    public static bool operator !=(CustomObject lhs, CustomObject rhs) {
        return !(lhs == rhs);
    }

    public static bool operator ==(CustomObject lhs, string rhs) {
        if (ReferenceEquals(lhs, rhs)) { return true; }
        if (ReferenceEquals(lhs, null)) { return false; }
        if (ReferenceEquals(rhs, null)) { return false; }
        return lhs._id == rhs;
    }

    public static bool operator !=(CustomObject lhs, string rhs) {
        return !(lhs==rhs);
    }

}

CustomObject o = null;
if (o == null) {
    Console.WriteLine("I don't compile.");
}
Josh Einstein
I was 2 minutes away from adding an answer with the `==` operator, which started as a comment to your answer. Any way, you got your +1 before.
Kobi
Thanks, great answer. In the project I mentioned they do use this same type of null check on concrete types as well.
Jace Rhea
But I should add that anyone that did this with a class should be hunted down and killed by the smoke monster. It would put a ton of undue stress and confusion on anyone that used the type.
Josh Einstein
@Kobi your anti-email harvesting tactic in your profile is great!
Josh Einstein