The difference between Struct and Class in C#

Original address: https://www.cnblogs.com/gsk99/archive/2011/05/20/1904552.html

The most essential difference between class and struct is that class is a reference type, while struct is a value type, and their allocation in memory is different.

What is class?

Class (class) is the basic concept of object-oriented programming, is a custom data structure type, usually contains fields, properties, methods, properties, constructors, indexers, operators and so on. In .NET, all classes ultimately inherit from the System.Object class, so it is a reference type, that is, when an instance of a new class is new, the instance is stored on the stack (stack) in the managed heap (managed heap). ), while the instance's value is stored in the managed heap.

What is struct?

A struct is a value type used to organize a group of related variables into a single variable entity. All structures inherit from the System.ValueType class and are therefore a value type, that is, a struct instance is allocated on the thread's stack when it is created, which itself stores the value. So when using struct, we can treat it as a basic type class such as int and char.

 

1 , class  is a reference type, structs is a value type

Since class is a reference type, class can be set to null . But we can't make struct null because it is a value type.

copy code
namespace Ax
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
            structA x = null ; // Error: Can't convert Null to 'structA' because it is a non-nullable type     
            classA y = null ;
        }
    }
}

public  struct structA
{
    public  int A;
}

public class classA
{
    public  int A;
}
copy code

 

2 , when you instantiate a class , it will be created on the heap. and you instantiate a struct , it will be created on the stack

3 , you are using a reference to an instance of the class . And you're not using a reference to a struct . (instead use them directly)

4,当我们将class作为参数传给一个方法,我们传递的是一个引用。struct传递的是值而非引用。

5structs 不可以有初始化器,class可以有初始化器。

copy code
public struct structA
{
    //public int A = 90; //错误:“structA.A”: 结构中不能有实例字段初始值
    public int A;

}

public class classA
{
    public int A = 90;
}
copy code

 

6Classes 可以有明显的无参数构造器,但是Struct不可以

copy code
public struct structA
{
    //public int A = 90; //错误:“structA.A”: 结构中不能有实例字段初始值
    public int A;
    //public structA() //错误:结构不能包含显式的无参数构造函数
    //{
    //    this.A = 0;
    //}

    public structA(int paraA) //ok
    {
        this.A = paraA;
    }
}

public class classA
{
    public int A = 90;
    public classA()
    {
        this.A = 90;
    }
}
copy code

 

7,类使用前必须new关键字实例化,Struct不需要

copy code
        public Form3()
        {
            InitializeComponent();
            //structA x = null; //错误    :无法将 Null 转换成“structA”,因为它是一种不可以为 null 值的类型    
            structA x;
            x.A = 8;
            structA x2 = new structA();
            x2.A = 32;
            classA y = null;
            classA y1 = new classA();
            y1.A = 4;
            classA y2;
            //y2.A = 5;//错误:使用了未赋值的局部变量“y2”
        }
copy code

 

8class支持继承和多态,Struct不支持注意:但是Struct 可以和类一样实现接口

9,既然Struct不支持继承,其成员不能以protected Protected Internal 修饰

10Class的构造器不需要初始化全部字段,Struct的构造器必须初始化所有字段

 

copy code
public struct structA
{
    public int A;
    public int B;

    public structA(int paraA, int paraB) //ok
    {
        this.A = paraA;
        this.B = paraB;
    }

    //public structA(int paraA) //ok
    //{
    //    this.A = paraA;
    //    this.B = paraA;
    //}

    //public structA(int paraA, int paraB) //错误:在控制返回调用方之前,字段“structA.B”必须被完全赋值
    //{
    //    this.A = paraA;
    //}
}

public class classA
{
    public int A = 90;
    public int B;
    public classA()
    {
        this.A = 90;
    }
}
copy code

 

 

11Class可以定义析构器,但是Struct不可以

12. Class is more suitable for large and complex data, and Struct is suitable for new types that are often used as a combination of some data .

 

Applicable occasions: Struct has performance advantages, Class has object-oriented extension advantages.

The type used for the underlying data store is designed to be Struct type, and the type used to define the application behavior is designed as a Class . If you are not sure about the future application of the type, you should use Class .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325713038&siteId=291194637