在什么情况下使用struct,struct与class的区别

类是引用类型,是保存在托管堆中的。通过定义类,我们可以在数据的生存期上得到很高的灵活性,但是也会让程序的性能有一定的损失。虽然这种损失很小,但当我们只需要定义一个很小的结构时,用类来定义就有些浪费,对于这样的问题,C#有相对应的方案来解决,那就是-结构(struct)。

结构(struct)是一种值类型。也就是说,结构实例是分配在线程堆栈上的,结构本身是包含有值的,而不是像类一样的引用类型,包含的是所指向堆当中的引用(指针)。也就是说,结构的生存周期与简单类型(int,double等)相同的。所以说我们在定义较小的类时,可以尽量使用结构。

结构与类的区别:

(1)结构是值类型,不是引用类型。

(2)结构可以继承接口,但是不可以继承类或结构。

(3)结构的构造方法的工作方式有所不同,只能声明带参数的构造方法,且不能声明析构方法。

(4)可以指定字段如何在内存中布局。

什么时候使用(struct)结构:

虽然我们可以用(class)类完全代替(struct)结构,但是为了程序的性能的提高,建议大家在实现一个用于存储数据或数据量较小的结构时来使用结构,因为结构是值类型,所以在性能的影响上,是能起到正面作用的。

MSDN中的定义:

A struct type is a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types.

Example 1

This example demonstrates struct initialization using both default and parameterized constructors.

// keyword_struct.cs

// struct declaration and initialization

using System;

public struct Point

{

   public int x, y;

   public Point(int p1, int p2)

  {

      x = p1;

      y = p2;   

   }

}

class MainClass

{

   public static void Main() 

   {

      // Initialize:  

      Point myPoint = new Point();

      Point yourPoint = new Point(10,10);

      // Display results:

      Console.Write("My Point:   ");

      Console.WriteLine("x = {0}, y = {1}", myPoint.x, myPoint.y);

      Console.Write("Your Point: ");

      Console.WriteLine("x = {0}, y = {1}", yourPoint.x, yourPoint.y);

   }

}

Output

My Point:   x = 0, y = 0

Your Point: x = 10, y = 10

Example 2

This example demonstrates a feature that is unique to structs. It creates a Point object without using the new operator. If you replace the word struct with the word class, the program won't compile.

// keyword_struct2.cs

// Declare a struct object without "new"

using System;

public struct Point

{

   public int x, y;

   public Point(int x, int y)

   {

      this.x = x;

      this.y = y;

   }

}

class MainClass

{

   public static void Main()

   {

      // Declare an object:

      Point myPoint;

      // Initialize:

      myPoint.x = 10;

      myPoint.y = 20;

      // Display results:

      Console.WriteLine("My Point:");

      Console.WriteLine("x = {0}, y = {1}", myPoint.x, myPoint.y);

   }

}

Output

My Point:

x = 10, y = 20

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

区别:详见http://www.cnblogs.com/gsk99/archive/2010/12/13/1904552.html

1,class 是引用类型,structs是值类型

既然class是引用类型,class可以设为null。但是我们不能将struct设为null,因为它是值类型。

struct AStruct

{

   int aField;

}

class AClass

{

   int aField;

}

class MainClass

{

 public static void Main()

 {

    AClass b = null; // No error.

    AStruct s = null; // Error [ Cannot convert null to 'AStruct'because it is a value type ].

 }

}

2,当你实例化一个class,它将创建在堆上。而你实例化一个struct,它将创建在栈上

3,你使用的是一个对class实例的引用。而你使用的不是对一个struct的引用。(而是直接使用它们)

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

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

class MyClass

{   

 int myVar =10; // no syntax error.   

 public void MyFun( )

 { // statements }

}

struct MyStruct

{   

 int myVar = 10; // syntax error.   

 public void MyFun( ) 

 {// statements }

}

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

class MyClass

{  

 int myVar = 10;

 public MyClass( ) // no syntax error. 

 {   

// statements

  }

}

struct MyStruct

 int myVar;

 public MyStruct( ) // syntax error.

   {      

               // statements 

   }

}

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

MyClass aClassObj;     // MyClass aClassObj=new MyClass(); is the correct format.aClassObj.

myVar=100;//NullReferenceException(because aClassObj does not contain a reference to an object of type myClass).

MyStruct aStructObj;

aStructObj.myVar=100; // no exception.

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

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

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

class MyClass    //No error( No matter whether the Field ' MyClass.myString ' is initialized or not ).

{

 int myInt; 

 string myString;  

 public MyClass( int aInt )

 {    myInt = aInt;    }

}

struct MyStruct    // Error ( Field ' MyStruct.myString ' must be fully assigned before it leaves the constructor ).

{

 int myInt; 

 string myString;

 public MyStruct( int aInt )

   {   

    myInt = aInt; 

   }

}

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

12,Class比较适合大的和复杂的数据,Struct适用于作为经常使用的一些数据组合成的新类型。

适用场合:Struct有性能优势,Class有面向对象的扩展优势。

用于底层数据存储的类型设计为Struct类型,将用于定义应用程序行为的类型设计为Class。如果对类型将来的应用情况不能确定,应该使用Class。

原文地址:https://www.cnblogs.com/niaowo/p/4502519.html