Default value check using generic types [duplicate

2020-06-12 03:39发布

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?

标签: c# generics
4条回答
叼着烟拽天下
2楼-- · 2020-06-12 04:20

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 :->

查看更多
可以哭但决不认输i
3楼-- · 2020-06-12 04:27
public bool EqualsDefaultValue<T>(T value)
{
    return EqualityComparer<T>.Default.Equals(value, default(T));
}
查看更多
Root(大扎)
4楼-- · 2020-06-12 04:31

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)))
    {

    }
}
查看更多
Evening l夕情丶
5楼-- · 2020-06-12 04:32

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));
}
查看更多
登录 后发表回答