Default value check using generic types [duplicate

2020-06-12 04:10发布

问题:

I want to be able to check whether a value is the default for its value type. Ideally, I'd like to say:

DoSomething<TValue>(TValue value) {
    if (value == default(TValue)) {
        ...
    }
}

However, the compiler complains that it can't do a == comparison on TValue and TValue. This is the best workaround that I've come up with so far:

DoSomething<TValue>(TValue value) {
    if (value == null || value.Equals(default(TValue))) {
        ...
    }
}

Is there a more elegant/correct way to go about this?

回答1:

public bool EqualsDefaultValue<T>(T value)
{
    return EqualityComparer<T>.Default.Equals(value, default(T));
}


回答2:

Throw in the class constraint and it should work.

public void Test<T>(T instance) where T : class
{
    if (instance == default(T))
    {

    }
}

Or if you only want value types you can do this.

public void Test<T>(T instance) where T : struct, IEquatable<T>
{
    if (instance.Equals(default(T)))
    {

    }
}


回答3:

Adding to the answers posted here, I think we should also be able to specify if you we want value or reference equality:

static public class MyGenericHelper
{
    static public bool EqualsByValue<T>(T x, T y)
    {
        return EqualityComparer<T>.Default.Equals(x, y);
    }

    static public bool EqualsByReference<T>(T x, T y)
    {
        if (x is ValueType) return EqualityComparer<T>.Default.Equals(x, y) // avoids boxing

        return Object.ReferenceEquals(x, y);
    }
}

We all just love to create and maintain a zillion little helper methods like that don't we :->



回答4:

Your problem is that a genric type (with no constraint) has to be "compilable" for any type. Since not all types have an == operator your code will not compile.

One way to resolve it is to add a class constraint however since your using default(TValue) that would suggest you want the code to work with other types. (otherwise just use null instead of default(TValue). One solution might be something similar to what Bryan Watts suggests

bool DoSomething<TValue>(TValue value) {
    return EqualityComparer<TValue>.Default.Equals(value, default(TValue));
}

or you could wrap it up in an extension method

bool IsDefault<TValue>(this TValue value) {
    return EqualityComparer<TValue>.Default.Equals(value, default(TValue));
}