-2

I'm a bit surprised to find that calling the constructor in:

class MyClass<T>
{
    public MyClass()
    {
        if ( default( T ) == null )
            Debugger.Break();
    }
}

doesn't break when T is an enum type. Why? It seems that even for enums, default( T ) and null should be equivalent.

HappyNomad
  • 4,458
  • 4
  • 36
  • 55
  • Why? Any link to documentation that made you believe so? (you may want to read http://stackoverflow.com/questions/65351/null-or-default-comparison-of-generic-argument-in-c-sharp if you need to compare parameters with "null") – Alexei Levenkov Jul 15 '16 at 01:08
  • See http://stackoverflow.com/questions/4967656/what-is-the-default-value-for-enum-variable – seairth Jul 15 '16 at 01:10
  • Resharper should have given you the warning. – Stephen Zeng Jul 15 '16 at 01:24

2 Answers2

2

No value type is ever going to test as equal to null, unless it's Nullable<T> which has special compiler and language support to treat an unset value as null.

The default(T) where T is any value type, including an enum, is going to be whatever the 0-filled value for that type is. I.e. an actual value. Not null.


Additional reading: How to set enum to null
What does default(object); do in C#?

You may also want to read through some of the other hits in this search: [c#] default value enum null

Community
  • 1
  • 1
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • Right. That's why the `default` keyword was introduced to C# to begin with. I just find it somewhat unintuitive that a "0-filled value" is different than `null`. So the reason wasn't immediately obvious. – HappyNomad Jul 15 '16 at 02:11
  • _"That's why the default keyword was introduced to C# to begin with"_ -- Actually, this _usage_ of the `default` keyword (there was always `default` in a `switch` statement) was introduced to support generic code that needed to be able to always create a default value without knowing the actual type. That this happens to wind up being the 0-filled value is as much a matter of convenience and efficiency as anything else. Similarly, that `null` is internally represented as `0` is a matter of convenience; `null` is a special value that has meaning only when dealing with reference types. – Peter Duniho Jul 15 '16 at 02:16
  • "`null` is a special value that has meaning only when dealing with reference types" -- That's precisely the part I find unintuitive. Upon deeper consideration, however, the difference is apparent. Whereas a value type's default value is still an actual value, a reference type's default value (`null`) is the *lack* of a value. – HappyNomad Jul 15 '16 at 02:51
  • @HappyNomad: `null` is a valid value for *references*. An instance of a reference *type* is never `null`, obviously. Distinguish the things from what points to them, then it's probably easier to understand and grasp. On the other hand, there are no references to value types, only values. Which is why `null` isn't a valid value there. – Joey Jul 15 '16 at 06:57
1

Because enum in .net is a value type. If you only want to find out if default(T) == null then you can check if it is a reference type instead.

class MyClass<T>
{
    public MyClass()
    {
        if (typeof (T).IsClass)
        {
            Debugger.Break();
        }
        else if (typeof (T).IsValueType)
        {
            //do something
        }
    }
}
Stephen Zeng
  • 2,748
  • 21
  • 18
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/13012680) – Gynteniuxas Jul 15 '16 at 05:43
  • Answer has been updated. Can someone cancel the down vote? – Stephen Zeng Jul 15 '16 at 06:50