C # struct class differences

Source: Internet
Author: User
Tags abstract assert bool expression garbage collection inheritance mscorlib volatile
C # Struct/class Differences
struct direct{//...}

Class indirect{//...}
Events are locked? Exist on stack or heap? Can cause garbage collection? Meaning of this? Always has a default constructor? Default construction triggers static construction? Can be null? Use with the as operator? Can be locked? Can have a destructor? Default field layout? Can be a volatile field? Can have synchronized methods? Can is pointed to? Can be stackalloc ' d? Can be sizeof ' d? How to initialize fields? Inheritance differences? Equals behavior Events are locked?
Events declared in a class have their + + = Access automatically locked via a lock (this) to make them thread safe (STA TIC events are locked on the TypeOf class). Events declared in a struct does not have their + + +-= Access automatically locked. A lock (this) is a struct would not work since to you can only lock on a reference type expression.
Exist on stack or heap?
The Value type local instances are allocated on the stack. Reference type local instances are allocated on the heap.
Can cause garbage collection?
Creating a struct instance cannot cause a garbage collection (unless the constructor directly or indirectly creates a Refe Rence type instance) whereas creating a reference type instance can cause garbage.
Meaning of this?
In a class, it is classified as a value, and thus cannot appear on the left hand side of a assignment, or be used as a Ref/out parameter. For example:

Class indirect{//... public void method (Indirect that) {refparameter (§REF);//Compile-time error outparameter (out T His); Compile-time Error This = that; Compile-time Error}//...}
In a struct, the This is classified as "a out parameter" in a constructor and as a ref parameter the all other function members. Thus It is possible to modify the entire structure from assigning to this or passing this as a ref/out parameter. For example:
struct direct{//... public void reassign (Direct so) {Refparameter (ref this);//compiles OK Outparameter (out);// Compiles OK this = that; Compiles OK}//...}
Furthermore, you can reassign a whole struct even when the struct contains readonly
struct direct{public Direct (int value) {Field = value;} public void Reassign (Direct,) {Refparameter (ref this);// Compiles OK Outparameter (out of this); Compiles OK this = that; Compiles OK} public readonly int Field;} Class show{static void Main () {Direct s = new Direct (42); Console.WriteLine (S.field); Writes S.reassign (new Direct (24)); Console.WriteLine (S.field); Writes 24}}
Note however then you call a method on a readonly value-type field, the "method" is made on a copy of the field.
struct direct{//as Above}class caller{public void Method () {Console.WriteLine (D.field);//Writes d.reassign (new Di Rect (24)); Console.WriteLine (D.field); Writes 42! Private ReadOnly Direct D = new Direct (42); }class show{static void Main () {Caller c = new Caller (); C.method ();}}


Always have a default constructor?
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 Indirec T (); Compiles if C ' Tor exists and is accessible//...}}
This means so a struct is always instantiable whereas a class might the not to since all its constructors could to be private.
Class noninstantiable{Private noninstantiable ()//OK {}}struct direct{private Direct ()//Compile-time error {}}


Default construction triggers static constructor?
A structs Static constructor is isn't triggered by calling the structs default constructor. It is for a class.

struct direct{static Direct () {Console.WriteLine ("This isn't written");} Class nottriggered{static void Main () {Direct Local = new Direct ();}}
Can be null?
A struct instance cannot be null.

Class nullness{static void Eg (Direct s, Indirect c) {if (s = = null) ...//Compile-time error if (c = null) ...//Comp Iles OK}}
Use with the as operator?
A struct type cannot is the right hand side operand of the as operator.

Class fragment{static void Eg (direct S, Indirect c) {Direct No = s as Direct;//compile-time error Indirect Yes = c AS InDirect; Compiles OK//...}}
Can be locked?
A struct type expression cannot be the operand of a lock statement.

Class lockstatement{static void Eg (Direct s, InDirect c) {lock (s) {...}//Compile-time error Lock (c) {...}//Compi Les OK}}
Can have a destructor?
A struct cannot have a destructor. A destructor is just A override of object. Finalize in disguise, and structs, being value types, are don't subject to GARABGE 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{//.
Default field layout?
The default [StructLayout] attribute (which lives in the System.Runtime.InteropServices namespace) for a struct is LAYOUTK Ind. Sequential whereas the default StructLayout for a class is Layoutkind.auto. (And yes, the despite its name can be tag a class with the StructLayout attribute.) In the "other words" CIL for this:

public struct direct{//...}

Looks like this:

. class public sequential ANSI sealed beforefieldinit Direct extends [mscorlib]system.valuetype{//...}

Whereas the CIL for this:

public sealed class indirect{//...}

Looks like this:

. class public auto ANSI sealed beforefieldinit Indirect extends [mscorlib]system.object{//...}
Can be a volatile field?
Can ' t declare a user-defined struct type as a volatile field but you can declare a user-defined class type as a Volati Le field.

Class bad{private volatile Direct field;//compile-time error}class good{private volatile Indirect field;//Compiles o K
Can have synchronized methods?
Can ' t use the [MethodImpl (methodimploptions.synchronized)] attribute to methods of a struct type (if you call the meth OD you get a runtime TypeLoadException) whereas can use the [MethodImpl (methodimploptions.synchronized)] attribute on Methods of a class type.

Using System.runtime.compilerservices;class indirect{[MethodImpl (methodimploptions.synchronized)]//Compiles and Runs OK public void method () {//...}} struct direct{[MethodImpl (methodimploptions.synchronized)]//compiles OK, runtime TypeLoadException public void method () { //... }}
Can is pointed to?
Clause 25.2 of the C # standard defines a unmanaged type as any type that isn ' t a reference type and doesn ' t contain Ence-type fields at no level of nesting. This is, one of the following:
Any simple value type (11.1.3, eg byte, int, long, double, bool, etc). any enum type. Any pointer type. Any user-defined struct-type that contains fields of unmanaged. You can never take the address of a instance of a type this is not unmanaged (a fixed variable 25.3).
Class bad{static void Main () {Indirect variable = new Indirect (); unsafe {fixed (Indirect * ptr = &variable)//Comp Ile-time error {//...}} }}
If you are want to fix a unmanaged instance you are have to doing so by fixing it through field. For example:
Class indirect{public int fixhandle;} Class bad{static void Main () {Indirect variable = new Indirect (); unsafe {fixed (int * ptr = &variable.fixhandle)// Compiles OK {//...}} }}
In contrast, your can (nearly) always take the address of a unmanaged instance.
struct direct{//No reference fields at any level of nesting}class simplecase{static void Main () {Direct variable = new Direct (); unsafe {Direct * ptr = &variable;//compiles OK//...}}}
However, you have to take the address inside a fixed statement if the variable was moveable (subject to relocation by the G Arbage Collector, 25.3 and example above). Also, you can never take to the address of a volatile field.


So, in summary, your can never create a pointer to a class type but your sometimes create a pointer to a struct type.
Can be stackalloc ' d?
You can only use stackalloc on unmanaged types. Hence can never use stackalloc on class types. For example:

Class indirect{//...} Class bad{static void Main () {unsafe {Indirect * array = stackalloc indirect[42];//Compile-time Error//...}}}
Where as can use stackalloc on struct types that are unmanaged. For example:
struct direct{//No reference fields at any level of nesting}class good{static void Main () {unsafe {Direct * array = s Tackalloc direct[42]; Compiles OK//...} }}


Can be sizeof ' d?
You can only use sizeof on unmanaged types. Hence can never use sizeof on class types. For example:

Class indirect{//...} Class bad{static void Main () {unsafe {int size = sizeof (Indirect);//Compile-time Error//...}}}
Where as can use sizeof on struct types that are unmanaged. For example:
struct direct{//No reference fields at any level of nesting}class good{static void Main () {unsafe {int size = sizeof ( Direct); Compiles OK//...} }}


How to initialize fields?
The fields of a class have a default initialization to Zero/false/null. The fields of a struct have no default value.

struct direct{public int Field;} Class indirect{Public Indirect () {}//... public int Field; Class defaults{static void Main () {Direct s; Console.WriteLine (S.field); Compile-time error Indirect C = new Indirect (); Console.WriteLine (C.field); Compiles OK}}



You can initialize fields in a class at their point of declaration. For example:

Class indirect{//... private int field = 42;}
You can ' t does this to fields in a struct. For example:
struct direct{//... private int field = N//Compile-time Error}
Fields in a struct have to be initialized in a constructor. For example:
struct direct{public Direct (int value) {field = value;}///... private int field;//compiles OK}
Also, the definite assignment rules of a struct are tracked on a individual field basis. This means can bypass initialization and "assign" the fields of a struct one. For example:
struct direct{public int X, Y;} Class example{static void Main () {Direct D; d.x = 42; Console.WriteLine (d.x); Compiles OK Console.WriteLine (D.Y); Compile-time Error}}


Inheritance differences?

A struct is implicitly sealed, a class isn ' t. A struct can ' t be abstract, a class can. A struct can ' t call:base () 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 (eg 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" is "that" a struct can override the virtual methods of System.Object, Viz, Equals (), and Geth Ashcode (), and ToString ().

Equals behavior?
Classes inherit object.equals which implements identity equality whereas structs inherit Valuetype.equals which implements Value equality.

Using system.diagnostics;struct direct{public Direct (int value) {field = value;} private int field;} Class indirect{public Indirect (int value) {field = value;} private int field;} Class equalsbehavior{static void Main () {Direct S1 = new Direct (42); Direct S2 = new Direct (42); Indirect C1 = new Indirect (42); Indirect C2 = new Indirect (42); BOOL structequality = S1. Equals (S2); BOOL classidentity =!c1. Equals (C2); Debug.Assert (structequality); Debug.Assert (classidentity); }}
Overriding Equals for structs should is faster because it can avoid reflection and boxing.
struct direct{public Direct (int value) {field = value;} public override bool Equals ECT && Equals ((Direct) other); public static bool operator== (direct LHS, direct RHS) {return LHS. Equals (RHS); ///... private bool Equals (Direct Other) {return field = Other.field;} private int field;}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.