tags:

views:

2980

answers:

8

I have a generic method defined like this:

public void MyMethod<T>(T myArgument)

The first thing I want to do is check if the value of myArgument is the default value for that type, something like this:

if (myArgument == default(T))

But this doesn't compile because I haven't guaranteed that T will implement the == operator. So I switched the code to this:

if (myArgument.Equals(default(T)))

Now this compiles, but will fail if myArgument is null, which is part of what I'm testing for. I can add an explicit null check like this:

if (myArgument == null || myArgument.Equals(default(T)))

Now this feels redundant to me. ReSharper is even suggesting that I change the myArgument == null part into myArgument == default(T) which is where I started. Is there a better way to solve this problem?

EDIT: I need to support both references types and value types.

A: 

do you need to support both ValueTypes and ReferenceTypes?

Nick Berardi
Yes, I need to support both value types and reference types.
Stefan Moser
+5  A: 

I was able to locate a Microsoft Connect article that discusses this issue in some detail:

Unfortunately, this behavior is by design and there is not an easy solution to enable use of with type parameters that may contain value types.

If the types are known to be reference types, the default overload of defined on object tests variables for reference equality, although a type may specify its own custom overload. The compiler determines which overload to use based on the static type of the variable (the determination is not polymorphic). Therefore, if you change your example to constrain the generic type parameter T to a non-sealed reference type (such as Exception), the compiler can determine the specific overload to use and the following code would compile:

public class Test<T> where T : Exception

If the types are known to be value types, performs specific value equality tests based on the exact types used. There is no good "default" comparison here since reference comparisons are not meaningful on value types and the compiler cannot know which specific value comparison to emit. The compiler could emit a call to ValueType.Equals(Object) but this method uses reflection and is quite inefficient compared to the specific value comparisons. Therefore, even if you were to specify a value-type constraint on T, there is nothing reasonable for the compiler to generate here:

public class Test<T> where T : struct

In the case you presented, where the compiler does not even know whether T is a value or reference type, there is similarly nothing to generate that would be valid for all possible types. A reference comparison would not be valid for value types and some sort of value comparison would be unexpected for reference types that do not overload.

Here is what you can do...

I have validated that both of these methods work for a generic comparison of reference and value types:

object.Equals(param, default(T))

or

EqualityComparer<T>.Default.Equals(param, default(T))

To do comparisons with the "==" operator you will need to use one of these methods:

If all cases of T derive from a known base class you can let the compiler know using generic type restrictions.

public void MyMethod<T>(T myArgument) with T : MyBase

The compiler then recognizes how to perform operations on MyBase and will not throw the "Operator '==' cannot be applied to operands of type 'T' and 'T'" error that you are seeing now.

Another option would be to restrict T to any type that implements IComparable.

public void MyMethod<T>(T myArgument) with T : IComparable

And then use the CompareTo method defined by the IComparable interface.

spoon16
+27  A: 

How about this:

if (object.Equals(myArgument, default(T)))
{
    //...
}

Using the static object.Equals() method avoids the need for you to do the null check yourself. Explicitly qualifying the call with object. probably isn't necessary depending on your context, but I normally prefix static calls with the type name just to make the code more soluble.

HTH, Kent

Kent Boogaart
You can even drop the "object." part since it's redundant. if (Equals(myArgument, default(T)))
Stefan Moser
True, it normally is, but may not be depending on the context. There may be an instance Equals() method that takes two arguments. I tend to explicitly prefix all static calls with the class name, if only to make the code easier to read.
Kent Boogaart
Need to note that it will cause boxing and in some cases it may be important
nightcoder
A: 

Don't know if this works with your requirements or not, but you could constrain T to be a Type that implements an interface such as IComparable and then use the ComparesTo() method from that interface (which IIRC supports/handles nulls) like this:

public void MyMethod<T>(T myArgument) where T : IComparable
...
if (0 == myArgument.ComparesTo(default(T)))

There are probably other interfaces that you could use as well IEquitable, etc.

caryden
+3  A: 

Try this:

if (EqualityComparer<T>.Default.Equals(myArgument, default(T)))

that should compile, and do what you want.

Lasse V. Karlsen
A: 

I've beaten this in the past by using Object.ReferenceEquals(obj, null). However, that adds the complexity of boxing. I've confirmed that this works in limited situations, but you might want to test it further to ensure that it works for you.

Note: Commenter is correct, won't work for value types (also I have a bug to fix)! :) Ignore this post.

Nate Kohari
That won't work with value-types of course.
Lasse V. Karlsen
A: 

@ilitirit:

public class Class<T> where T : IComparable
{
    public T Value { get; set; }
    public void MyMethod(T val)
    {
     if (Value == val)
      return;
    }
}

Operator '==' cannot be applied to operands of type 'T' and 'T'

I can't think of a way to do this without the explicit null test followed by invoking the Equals method or object.Equals as suggested above.

You can devise a solution using System.Comparison but really that's going to end up with way more lines of code and increase complexity substantially.

cfeduke
A: 

I think you probably need to split this logic into two parts and check for null first.

public static bool IsNullOrEmpty<T>(T value)
{
    if (IsNull(value))
    {
        return true;
    }
    if (value is string)
    {
        return string.IsNullOrEmpty(value as string);
    }
    return value.Equals(default(T));
}

public static bool IsNull<T>(T value)
{
    if (value is ValueType)
    {
        return false;
    }
    return null == (object)value;
}

In the IsNull method, we're relying on the fact that ValueType objects can't be null by definition so if value happens to be a class which derives from ValueType, we already know it's not null. On the other hand, if it's not a value type then we can just compare value cast to an object against null. We could avoid the check against ValueType by going straight to a cast to object, but that would mean that a value type would get boxed which is something we probably want to avoid since it implies that a new object is created on the heap.

In the IsNullOrEmpty method, we're checking for the special case of a string. For all other types, we're comparing the value (which already know is not null) against it's default value which for all reference types is null and for value types is usually some form of zero (if they're integral).

Using these methods, the following code behaves as you might expect:

class Program
{
    public class MyClass
    {
        public string MyString { get; set; }
    }

    static void Main()
    {
        int  i1 = 1;    Test("i1", i1); // False
        int  i2 = 0;    Test("i2", i2); // True
        int? i3 = 2;    Test("i3", i3); // False
        int? i4 = null; Test("i4", i4); // True

        Console.WriteLine();

        string s1 = "hello";      Test("s1", s1); // False
        string s2 = null;         Test("s2", s2); // True
        string s3 = string.Empty; Test("s3", s3); // True
        string s4 = "";           Test("s4", s4); // True

        Console.WriteLine();

        MyClass mc1 = new MyClass(); Test("mc1", mc1); // False
        MyClass mc2 = null;          Test("mc2", mc2); // True
    }

    public static void Test<T>(string fieldName, T field)
    {
        Console.WriteLine(fieldName + ": " + IsNullOrEmpty(field));
    }

    // public static bool IsNullOrEmpty<T>(T value) ...

    // public static bool IsNull<T>(T value) ...
}
Damian Powell