c#中结构体和类的比较

翻了下书,总结一下。

区别:

结构是一种用关键字struct声明的自定义数据类型。与类相似,也可以包含构造函数,常数,字段,方法,属性,索引器,运算符和嵌套类型等,不过,结构是值类型。

1.结构的构造函数和类的构造函数不同。

   a.结构不能包含显式的无参数构造函数。结构成员讲自动初始化为它们的默认值。

   b.结构不能包含以下形式的初始值设定类:base(argument-list);

2.对于结构中的实例字段成员,不能在声明时赋值初始化。

3.声明了结构类型后,可以使用new运算符创建构造对象,也可以不使用new关键字。如果不使用new,那么在初始化所有字段之前,字段将保持未赋值状态且对象不可用。

4.结构不支持继承,即一个结构不能从另一个结构或类继承,而且不能作为一个类的基类。但是,结构从基类OBJECT继承。结构也可以实现接口。

5.什么时候用结构呢?结构使用简单,并且很有用,但是要牢记:结构在堆栈中创建,是值类型,而类是引用类型。每当需要一种经常使用的类型,而且大多数情况下该类型只是一些数据时,使用结构能比使用类获得更佳性能。

最后引用博客园的“越过林子”的话:

   结构是值类型,所以会影响性能,但根据使用结构的方式,这种影响可能是正面的,也可能是负面的。正面的影响是为结构分配内存时,速度非常快,因为它们将内联或者保存在堆栈中。在结构超出了作用域被删除时,速度也很快。另一方面,只要把结构作为参数来传递或者把一个结构赋给另一个结构(例如A=B,其中A和B是结构),结构的所有内容就被复制,而对于类,则只复制引用。这样,就会有性能损失,根据结构的大小,性能损失也不同。注意,结构主要用于小的数据结构。但当把结构作为参数传递给方法时,就应把它作为ref参数传递,以避免性能损失——此时只传递了结构在内存中的地址,这样传递速度就与在类中的传递速度一样快了。另一方面,如果这样做,就必须注意被调用的方法可以改变结构的值。

结构体的定义:

结构体也可以象类一样可以单独定义.
class  a{};
struct a{};

结构体也可以在名字前面加入控制访问符.
public struct student{};
internal struct student{};

如果结构体student没有publice或者internal的声明 类program就无法使用student结构定义 obj对象
如果结构体student的元素没有public的声明,对象obj就无法调用元素x
因为默认的结构体名和元素名是*******类型
程序:
using System;
public struct student
    {
       
public int x;
    };

class program
{
    
public static void Main()
    {
     student obj
=new student();
     obj.x
=100;       
    }

};
在结构体中也可以定义静态成员与类中一样,使用时必须用类名,或结构名来调用不属于实例,声明时直接定义.
程序:
using System;
public struct student
    {
     
public static int a = 10;
    };
class exe
{
 
public static void Main()
    {
     Console.WriteLine( student.a 
= 100);
    }
};

using System;
class base
{
public struct student
    {
     
public static int a = 10;
    };
}
class exe
{
 
public static void Main()
    {
     Console.WriteLine( 
base.student.a = 100);
    }
};
在结构体中可以定义构造函数以初始化成员,但不可以重写默认无参构造函数和默认无参析构函数
程序:
public struct student
    {
       
public int x;
       
public int y;
       
public static int z;
       
public student(int a,int b,int c)
        {
            x
=a;
            y
=b;
         student.z
=c;
        }

    };
在结构体中可以定义成员函数。
程序:
public struct student
    {
       
public void list()
            {
Console.WriteLine(
"这是构造的函数");
            }

     };
结构体的对象使用new运算符创建(obj)也可以直接创建单个元素赋值(obj2)这是与类不同的因为类只能使用new创建对象
程序:
public struct student
    {
       
public int x;
       
public int y;
       
public static int z;
       
public student(int a,int b,int c)
        {
            x
=a;
            y
=b;
         student.z
=c;
        }

    };
class program
{
 
public static void Main()
{
  student obj
=new student(100,200,300);
  student obj2;
  obj2.x
=100;
  obj2.y
=200;
  student.z
=300;
}
}

在使用类对象和函数使用时,使用的是引用传递,所以字段改变
在使用结构对象和函数使用时,是用的是值传递,所以字段没有改变

程序:
using System;
class class_wsy
{
    
public int x;
}
struct struct_wsy
{
    
public int x;
}
class program
{
    
public static void class_t(class_wsy obj)
    {
        obj.x 
= 90;
    }
    
public static void struct_t(struct_wsy obj)
    {
        obj.x 
= 90;
    }
    
public static void Main()
    {
        class_wsy obj_1 
= new class_wsy();
        struct_wsy obj_2 
= new struct_wsy();
        obj_1.x 
= 100;
        obj_2.x 
= 100;
        class_t(obj_1);
        struct_t(obj_2);
        Console.WriteLine(
"class_wsy obj_1.x={0}",obj_1.x);
        Console.WriteLine(
"struct_wsy obj_2.x={0}",obj_2.x);
        Console.Read();
    }
}
结果为:class_wsy obj_1.x
=90
       struct_wsy obj_2.x
=100

----------------------------------------------------------------------------------------------------------------------------------------------

另:

正确

 public StructTest(int i)
        {
            Name = i;
        }
        public int Name;

 StructTest stest;

stest.i=12;

错误

 public StructTest(int i)
        {
            Name = i;
        }
        public int Name{get;set;};

StructTest stest;

stest.i=12;

原文地址:https://www.cnblogs.com/zxktxj/p/2485658.html