Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why cast null before checking if object is equal to null?

Tags:

c#

null

casting

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?

like image 887
Jace Rhea Avatar asked May 23 '10 04:05

Jace Rhea


People also ask

What happens if you cast a null?

You can cast null to any reference type without getting any exception. The println method does not throw null pointer because it first checks whether the object is null or not. If null then it simply prints the string "null" . Otherwise it will call the toString method of that object.

Can you cast null C#?

1 Answer. Show activity on this post. According to the documentation (Explicit conversions) you can cast from a base type to a derived type. Since null is a valid value for all reference types, as long as the cast route exists you should be fine.

IS null check in C#?

In C#, IsNullOrEmpty() is a string method. It is used to check whether the specified string is null or an Empty string. A string will be null if it has not been assigned a value. A string will be empty if it is assigned “” or String.


1 Answers

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.");
}
like image 129
Josh Avatar answered Oct 12 '22 01:10

Josh