Differences between class, struct and enum

From Pigmeo Development Wiki

Jump to: navigation, search

Contents

CIL declaration

Class

.class public auto ansi beforefieldinit ClassName extends [mscorlib]System.Object { }
  • Static class:
.class public abstract auto ansi sealed beforefieldinit ClassName extends [mscorlib]System.Object { }

Struct

.class public sequential ansi sealed beforefieldinit StructName extends [mscorlib]System.ValueType { }

Enum

.class public auto ansi sealed EnumName extends [mscorlib]System.Enum {
    .field public static literal valuetype NamespaceName.EnumName First = int32(0)
    .field public static literal valuetype NamespaceName.EnumName Second = int32(1)
    .field public static literal valuetype NamespaceName.Enumname Third = int32(2)
    .field public specialname rtspecialname int32 value__
}


Keywords

Main article: Type Header

Note: quoted text was taken from the ECMA-335 specs.

  • ansi - Interoperation with unmanaged code. Ignore
  • beforefieldinit - "It need not initialize the type before a static method is called". Classes with explicit static constructors are not beforefieldinit. If not marked beforefieldinit the static constructor is called before ANY access to the class (static, instance or virtual fields or members). If marked beforefieldinit the static constructor is called before accessing any static field or method.
  • sealed - "A type shall not have derived classes. All value types shall be sealed". Ignore
  • literal - "Specifies metadata field. No memory is allocated at runtime for this field"

Layout

  • auto - "The layout shall be done by the CLI, with no user-supplied constraints". "The default auto layout should provide the best layout for the platform on which the code is executing."
  • explicit - "The layout of the fields is explicitly provided. However, a generic type shall not have explicit layout". "Explicit layout allows the CIL generator to specify the precise layout semantics."
  • sequential - "The CLI shall lay out the fields in sequential order, based on the order of the fields in the logical metadata table". "Sequential layout is intended to instruct the CLI to match layout rules commonly followed by languages like C and C++ on an individual platform, where this is possible while still guaranteeing verifiable layout.
Personal tools