What's the difference between struct and class in .NET?

前端 未结 19 1386
一向
一向 2020-11-22 01:51

What\'s the difference between struct and class in .NET?

19条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 02:02

    1. Events declared in a class have their += and -= access automatically locked via a lock(this) to make them thread safe (static events are locked on the typeof the class). Events declared in a struct do not have their += and -= access automatically locked. A lock(this) for a struct would not work since you can only lock on a reference type expression.

    2. Creating a struct instance cannot cause a garbage collection (unless the constructor directly or indirectly creates a reference type instance) whereas creating a reference type instance can cause garbage collection.

    3. A struct always has a built-in public default constructor.

      class DefaultConstructor
      {
          static void Eg()
          {
              Direct     yes = new   Direct(); // Always compiles OK
              InDirect maybe = new InDirect(); // Compiles if constructor exists and is accessible
              //...
          }
      }
      

      This means that a struct is always instantiable whereas a class might not be since all its constructors could be private.

      class NonInstantiable
      {
          private NonInstantiable() // OK
          {
          }
      }
      
      struct Direct
      {
          private Direct() // Compile-time error
          {
          }
      }
      
    4. A struct cannot have a destructor. A destructor is just an override of object.Finalize in disguise, and structs, being value types, are not subject to garbage collection.

      struct Direct
      {
          ~Direct() {} // Compile-time error
      }
      class InDirect
      {
          ~InDirect() {} // Compiles OK
      }
      
      And the CIL for ~Indirect() looks like this:
      
      .method family hidebysig virtual instance void
              Finalize() cil managed
      {
        // ...
      } // end of method Indirect::Finalize
      
    5. A struct is implicitly sealed, a class isn't.
      A struct can't be abstract, a class can.
      A struct can't call : base() in its constructor whereas a class with no explicit base class can.
      A struct can't extend another class, a class can.
      A struct can't declare protected members (for example, fields, nested types) a class can.
      A struct can't declare abstract function members, an abstract class can.
      A struct can't declare virtual function members, a class can.
      A struct can't declare sealed function members, a class can.
      A struct can't declare override function members, a class can.
      The one exception to this rule is that a struct can override the virtual methods of System.Object, viz, Equals(), and GetHashCode(), and ToString().

提交回复
热议问题