C# struct & class Differences

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 be 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 += 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. 

Exist on stack or heap?
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 reference type instance) whereas creating a reference type instance can cause garbage collection. 

Meaning of this?
In a class, this is classified as a value, and thus cannot appear on the left hand side of an assignment, or be used as a ref/out parameter. For example: 

class Indirect
{
    //...
    public void Method(Indirect that)
    {
        RefParameter(ref this); // compile-time error
        OutParameter(out this); // compile-time error
        this = that;            // compile-time error
    }
    //...
}
In a struct, this is classified as an out parameter in a constructor and as a ref parameter in all other function members. Thus it is possible to modify the entire structure by assigning to this or passing this as a ref/out parameter. For example: 
struct Direct
{
    //...
    public void Reassign(Direct that)
    {
        RefParameter(ref this); // compiles ok
        OutParameter(out this); // compiles ok
        this = that;            // compiles ok
    }
    //...
}
Furthermore, you can reassign a whole struct even when the struct contains readonly fields! 
struct Direct
{
    public Direct(int value)
    {
        Field = value;
    }
     
    public void Reassign(Direct that)
    {
        RefParameter(ref this); // compiles ok
        OutParameter(out 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 42
        s.Reassign(new Direct(24));
        Console.WriteLine(s.Field); // writes 24
    }
}
Note however that when you call a method on a readonly value-type field, the method call is made on a copy of the field. 
struct Direct
{
    // as above
}

class Caller
{
    public void Method()
    {
        Console.WriteLine(d.Field); // writes 42
        d.Reassign(new Direct(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 InDirect(); // compiles if c’tor 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
    {
    }
}

Default construction triggers static constructor?
A structs static constructor is not triggered by calling the structs default constructor. It is for a class. 

struct Direct
{
    static Direct() 
    {
        Console.WriteLine("This is not 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) ... // compiles ok
    }
}
Use with the as operator?
A struct type cannot be 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) { ... } // compiles ok
    }
}
Can have a destructor?
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 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
{
  // ...
} // end of method Indirect::Finalize
Default field layout?
The default [StructLayout] attribute (which lives in the System.Runtime.InteropServices namespace) for a struct is LayoutKind.Sequential whereas the default StructLayout for a class is LayoutKind.Auto. (And yes, despite its name you can tag a class with the StructLayout attribute.) In other words the 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?
You can’t declare a user-defined struct type as a volatile field but you can declare a user-defined class type as a volatile field. 

class Bad
{
    private volatile Direct field; // compile-time error
}
class Good
{
    private volatile Indirect field; // compiles ok
}
Can have synchronized methods?
You can’t use the [MethodImpl(MethodImplOptions.Synchronized)] attribute on methods of a struct type (if you call the method you get a runtime TypeLoadException) whereas you 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 be pointed to?
Clause 25.2 of the C# standard defines an unmanaged type as any type that isn’t a reference type and doesn’t contain reference-type fields at any level of nesting. That 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 types only. 
You can never take the address of a instance of a type that is not unmanaged (a fixed variable 25.3). 
class Bad
{
    static void Main()
    {
        Indirect variable = new Indirect();
        unsafe 
        { 
            fixed(Indirect * ptr = &variable) // compile-time error
            { 
                //...
            } 
        } 
    }
}
If you want to fix an unmanaged instance you have to do so by fixing it through an unmanaged 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, you can (nearly) always take the address of an 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 is moveable (subject to relocation by the garbage collector, see 25.3 and example above). Also, you can never take the address of a volatile field. 

So, in summary, you can never create a pointer to a class type but you sometimes create a pointer to a struct type. 

Can be stackalloc’d?
You can only use stackalloc on unmanaged types. Hence you 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 you 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 = stackalloc Direct[42]; // compiles ok
            //...
        }
    }
}

Can be sizeof’d?
You can only use sizeof on unmanaged types. Hence you 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 you 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 do this for fields in a struct. For example: 
struct Direct
{
    //...
    private int field = 42; // 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 an individual field basis. This means you can bypass initialization and "assign" the fields of a struct one a time. 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() 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 (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 this rule is that a struct can override the virtual methods of System.Object, viz, Equals(), and GetHashCode(), 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 be faster because it can avoid reflection and boxing. 
struct Direct
{
    public Direct(int value)
    {
        field = value;
    }
    
    public override bool Equals(object other)
    {
        return other is Direct && 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;
}

时间: 2024-08-07 06:49:33

C# struct & class Differences的相关文章

C# struct class Differences

C# struct/class Differencesstruct 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

.NET (C#) Internals: Struct and Class

引言 Struct与Class的异同?本是一个老生常谈话题,前几天看帖就看到了Struct 与Class辨析,其中也提到了<[你必须知道的.NET] 第四回:后来居上:class和struct>(虽然在园子里看了这个系列,但仍然买了本书看),回帖也特别热闹.我也躺下这个浑水!希望能给您带来不一样的视觉,欢迎评论.本文主题如下: 直观印象 深入分析  刨根问底(刨祖坟) 特别之处ReadOnly 浅出 一.直观印象 Struct与Class的异同,到底什么是什么呢?首先来两段代码,给个直观印象.

内存管理-如何解决C#向C++DLL传递 struct数组时产生的数据丢失问题?

问题描述 如何解决C#向C++DLL传递 struct数组时产生的数据丢失问题? 运行环境:win8.1+vs2010 目的:将C#中的结构体数组传到C++的动态链接库中 代码: //DLL中的接口,C++ struct Target { int ID; double PosX double PosY; float Aangle; long EncoderValue; }; class Backstage { EXTERN_C BACKSTAGE_API int WINAPI Allocater

使用C#拷贝String到struct

使用C#拷贝String到struct By dgiljr 介绍 本文介绍使用C#拷贝String到struct . 代码 using System;using System.Runtime.InteropServices;using System.Text; class Class1{ [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] public struct MyStruct { [MarshalAs(Unmana

C++中数据对齐问题:struct、union、enum。再谈sizeof()

首先是struct,在C++中,结构体其实和class有很大的相似了.但是有一点不同的是,struct默认是public,而class中是private. 当然,struct继承等用法也是可以的. 共用体的声明方式是: 枚举的声明方式与共用体比较相似 其中a初始化为0,后面默认增1,若已经初始化,则后面再增1,比如d=6在这里. struct长度计算 大家猜一下,s1 x;int b=sizeof(x); 他的结果会是多少呢?有人会觉得应该是1+8+4+1=14. 实际上是24.为什么会是这样呢

由struct的静态构造函数说起

最近才知道struct和class的静态构造函数的触发规则是不同的,不像class在第一次使用类的时候触 发静态构造函数.如果只访问struct实例的字段是不会触发静态构造函数调用的.通过测试发现当访问静 态字段,struct本身的函数(静态和实例)和带参数的构造函数就会引起静态构造函数的执行.而调用默 认构造和未覆写的基类虚函数是不会的.为什么呢? 让我们先来看看class和struct在调用构造函数时的区别.class使用newobj指令而struct使用initobj 指令来构造对象.ne

控件如何传递自定义struct

一.开发工具 我本来想用的是.ocx控件,最后发现,对于自定义struct,它似乎是无能为力(有谁知道.ocx可以的话请联系我).最后采用的是vc++6.0中的ATL.下面给出了如何调用含自定义结构的组件函数senddata,ReceiveData.组件中的参数传递与下面的代码是一模一样的 ,这里不再重复.(该组件是应用于配电自动化中的,用以实现馈线自动化FA功能,本人自己设计开发). 二.源码 这里介绍如何向控件传递自己的结构.调用组件函数的源代码如下: int result; VARIANT

Unix编程之int fstat(int fildes,struct stat *buf);

/* * fstat(由文件描述词取得文件状态) * 相关函数 stat,lstat,chmod,chown,readlink,utime * 表头文件 * #include<sys/stat.h> * #include<unistd.h> * 定义函数 * int fstat(int fildes,struct stat *buf); * 函数说明 fstat()用来将参数fildes所指的文件状态,复制到参数buf所指的 结构中(struct stat). * Fstat()与

linux内核不支持struct vm_area_struct结构体中flag标志使用值问题

error: 'VM_RESERVED' undeclared (first use in this function 从linux 3.7.0开始内核不再支持struct vm_area_struct结构体中flag标志使用值 VM_RESERVED,驱动开发中把 vma->vm_flags |= (VM_IO | VM_LOCKED | VM_RESERVED); 改为 vma->vm_flags |= (VM_IO | VM_LOCKED | (VM_DONTEXPAND | VM_DO