74

How to check programmatically if a type is a struct or a class?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jader Dias
  • 88,211
  • 155
  • 421
  • 625

8 Answers8

99

Use Type.IsValueType:

Gets a value indicating whether the Type is a value type.

Use it either like this:

typeof(Foo).IsValueType

or at execution time like this:

fooInstance.GetType().IsValueType

Conversely there is also a Type.IsClass property (which should have been called IsReferenceType in my opinion but no matter) which may or may not be more appropriate for your uses based on what you are testing for.

Code always seems to read better without boolean negations, so use whichever helps the readability of your code.


As Stefan points out below, in order to properly identify structs you must be careful to avoid false positives when it comes to enums. An enum is a value type so the IsValueType property will return true for enums as well as structs.

So if you truly are looking for structs and not just value types in general you will need to do this:

Type fooType = fooInstance.GetType();
Boolean isStruct = fooType.IsValueType && !fooType.IsEnum;
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 26
    A primitive type is also a value type. – Stefan Steinegger Dec 01 '09 at 16:55
  • 3
    @Stephan - It is true that all C# primitives happen to be value types but that does not mean that all value types are therefore C# primitives. `System.Guid` and `System.DateTime` are both value types but are not language primitives. – Andrew Hare Dec 01 '09 at 16:57
  • @Andrew: yes, Guid and DateTime are structs. See my answer. – Stefan Steinegger Dec 01 '09 at 16:57
  • @Stephan - You lost me. Are you saying that there is a difference between structs and value types? – Andrew Hare Dec 01 '09 at 16:59
  • Structs are simply one way that C# allows you to create value types in the resulting IL, enums are another way. – Andrew Hare Dec 01 '09 at 17:01
  • @Andrew: Structs are one kind of value types. Primitive types are another. So structs are value type, but value types not structs. (A bird is an animal, an animal is not a bird) – Stefan Steinegger Dec 01 '09 at 17:02
  • @Stephan - So would you consider `System.Int32` a value type, a struct, or a primitive? – Andrew Hare Dec 01 '09 at 17:05
  • @Andrew: int is a primitive type, which is a special case of a value type. – Stefan Steinegger Dec 01 '09 at 17:07
  • 2
    To expand my point, the term "primitive" is special and really only is reserve for certain types that have overridden the `IsPrimitiveImpl` method from `System.Type`. There is nothing stopping Microsoft from implementing a new primitive that happens to be a reference type. There is nothing about a primitive that necessitates that it must also be a value type. – Andrew Hare Dec 01 '09 at 17:10
  • The fact that all primitive types in mscorlib happen to be value types is either a design decision or a coincidence. Since the `IsPrimitiveImpl` method is defined in `System.Type` it can freely be implemented by a reference type as well. Only if this method were implemented on `System.ValueType` would you be able to say that all primitive types must be value types. The fact that all primitive types *that are currently defined* are value types is simply a coincidence. – Andrew Hare Dec 01 '09 at 17:13
  • 1
    @Andrew: I see, primitive types are actually defined as special case of structs, so I'm wrong. I added a note to my answer. I apologize for the troubles. – Stefan Steinegger Dec 01 '09 at 18:12
  • 1
    @Stephan - No trouble at all! A lively debate is good for everyone :) – Andrew Hare Dec 01 '09 at 18:13
  • 1
    Funny enough, in the C# reference here http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx an enum is actually not defined as a struct. Quote: "The value types consist of two main categories: structs, enumerations". While "numeric types" are defined as structs. Doesn't sound logical. – Stefan Steinegger Dec 01 '09 at 18:50
  • 7
    Correct, enums and structs are the two value types that C# supports. A helpful way to remember this is that a struct is a kind of value type, not the other way around. – Andrew Hare Dec 01 '09 at 18:52
  • So the correct answer to the question here should be: `type.IsValueType && !type.IsEnum` – Stefan Steinegger Dec 02 '09 at 21:11
  • @Stefan - Excellent point! The OP does ask specifically about *structs*, not value types - I will edit my answer. – Andrew Hare Dec 02 '09 at 23:58
  • Calling `GetType()` on a storage location will not reliably report whether the location holds a value type or a reference to a class-type instance. A boxed value type is actually a class-type object with the same fields and methods as its value-type namesake. If one has a field of type `IEnumerable` which holds an instance of the struct `List.Enumerator`, calling `GetType().IsValueType` on that field will return true, even though the object referred to by the field will behave like a class-type instance (under the covers it will actually be one). – supercat Feb 26 '12 at 19:21
  • 1
    I might add, within the metadata, the Primitive concept is special because the actual metadata defining the use of those primitive types, in every way, and their interpretation is special. Primitive types aren't outlined in metadata as `System.Int32`, or `System.Double`, but rather a special native type with unique bytes that specify `Int32` (`ELEMENT_TYPE_I4` or `0x08`) and `Double` (`ELEMENT_TYPE_R8` or `0x0D`.) I only know because I've had to write a metadata parser. – Allen Clark Copeland Jr Nov 28 '15 at 21:18
  • 1
    I need to serialize some data and this method does not work as it thinks a float is a struct. I need to recursively serialize structs down to their primitives but some structs can have other structs as fields. – Nick Sotiros Mar 15 '18 at 20:45
42
Type type = typeof(Foo);

bool isStruct = type.IsValueType && !type.IsPrimitive;
bool isClass = type.IsClass;

It could still be: a primitive type or an interface.


Edit: There is a lot of discussion about the definition of a struct. A struct and a value type are actually the same, so IsValueType is the correct answer. I usually had to know whether a type is a user defined struct, this means a type which is implemented using the keyword struct and not a primitive type. So I keep my answer for everyone who has the same problem then me.


Edit 2: According to the C# Reference, enums are not structs, while any other value type is. Therefore, the correct answer how to determine if a type is a struct is:

bool isStruct = type.IsValueType && !type.IsEnum;

IMHO, the definition of a struct is more confusing then logical. I actually doubt that this definition is of any relevance in praxis.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
  • The primitive type thing has been discussed to death in the comments of my answer :) You don't have to worry about an interface because and interface type instance will return `false` for both `IsClass` and `IsValueType`. Also any type that implements the interface will return its true type regardless of what the reference to the type is typed as an interface or not. – Andrew Hare Dec 01 '09 at 17:26
  • 1
    @Stefan: Are you saying that primitive types are precluded from being structs? If so, you're incorrect. For example, section 11 of the C# spec says "the simple types provided by C#, such as `int`, `double`, and `bool`, are in fact all `struct` types". – LukeH Dec 01 '09 at 17:27
  • 1
    @Luke: Yes, this is indeed the definition of primitive types. I always had to know if a type is a *user defined struct*, excluding primitive types, which is normally simply referenced to as "struct". But you are right, strictly speaking primitive types are also structs. – Stefan Steinegger Dec 01 '09 at 17:57
  • `type.IsValueType && !type.IsPrimitive` will not determine if the struct is a user-defined struct created using the keyword `struct`. It will return false positives for example for `decimal`, `int?`, or any enum. – thepirat000 Apr 23 '18 at 17:50
  • @thepirat000: Yes. There is no concept of "user-defined struct" in C#. Decimals and nullables are structs like any other. Of course you can exclude them explicitly, if it makes sense. There is a related (but different!) question with a better answer here: https://stackoverflow.com/questions/863881/how-do-i-tell-if-a-type-is-a-simple-type-i-e-holds-a-single-value/863944#863944 – Stefan Steinegger Apr 24 '18 at 12:36
4

Extension method. It returns true for anything defined as a struct in my code but not for things like int which although they are technically structs are not for my purposes.

I needed to know when a type may have child fields or properties but was defined as a struct and not a class. Because when you alter a struct it just alters a copy, and then you have to set the original back to the altered copy to make the changes "stick".

public static bool IsStruct(this Type source) 
{
  return source.IsValueType && !source.IsPrimitive && !source.IsEnum;
}
toddmo
  • 20,682
  • 14
  • 97
  • 107
2

I think it would be something like this:

Is it a structure

public bool IsStructure(Type LocalType)
{
    bool result = false;
    if (LocalType.IsValueType)
    {
        //Is Value Type
        if (!LocalType.IsPrimitive)
        {
            /* Is not primitive. Remember that primitives are:
            Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32,
            Int64, UInt64, IntPtr, UIntPtr, Char, Double, Single.
            This way, could still be Decimal, Date or Enum. */
            if (!LocalType == typeof(decimal))
            {
                //Is not Decimal
                if (!LocalType == typeof(DateTime))
                {
                    //Is not Date
                    if (!LocalType.IsEnum)
                    {
                        //Is not Enum. Consequently it is a structure.
                        result = true;
                    }
                }
            }
        }
    }
    return result;
}

Is it a Class

public bool IsClass(Type LocalType)
{
    bool result = false;
    if (LocalType.IsClass)
    {
        //Is Class or Delegate
        if (LocalType.GetType != typeof(Delegate))
            result = true;
    }
    return result;
}
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
1

Try the following

bool IsStruct(Type t) {
  return t.IsValueType;
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
1

The struct:

// determine if the type is a struct..
var isStruct = type.IsValueType && !type.IsEnum &&
               !type.IsEquivalentTo(typeof(decimal)) && 
               !type.IsPrimitive;

The class:

var isClass = type.IsClass;

The answer:

var isClassOrStruct = isStruct | isClass;
0
    //I think it would be something like this:

    public sbyte IsStructure(Type LocalType)
    {
            sbyte result = false;
            if (LocalType.IsValueType)
            {
                    //Is Value Type
                    if (!LocalType.IsPrimitive)
                    {
                            /* Is not primitive. Remember that primitives are:
                            Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32,
                            Int64, UInt64, IntPtr, UIntPtr, Char, Double, Single.
                            This way, could still be Decimal, Date or Enum. */
                            if (!LocalType == typeof(decimal))
                            {
                                    //Is not Decimal
                                    if (!LocalType == typeof(DateTime))
                                    {
                                            //Is not Date
                                            if (!LocalType.IsEnum)
                                            {
                                                    //Is not Enum. Consequently it is a structure.
                                                    result = true;
                                            }
                                    }
                            }
                    }
            }
            return result;
    }
-1

For every value type, there is a corresponding auto-generated class type which derives from System.ValueType, which in turn derives from System.Object. Note that value types themselves do not derive from anything, but are implicitly convertible to that class type, and instances of that class type may be explicitly converted to the value type.

Consider:

        public static int GetSomething<T>(T enumerator) where T : IEnumerator<int>
        {
            T enumerator2 = enumerator;
            enumerator.MoveNext();
            enumerator2.MoveNext();
            return enumerator2.Current;
        }

Calling this routine on a variable of type List<int>.Enumerator will yield very different behavior from calling it on a variable of type IEnumerator<int> which happens to have an instance of List<int>.Enumerator stored within it. Even though a variable of type List<int>.Enumerator is a value type, an instance of List<int>.Enumerator stored in a variable of type IEnumerator<int> will behave as a class type.

supercat
  • 77,689
  • 9
  • 166
  • 211