類是引用類型,是保存在托管堆中的。通過定義類,我們可以在數據的生存期上得到很高的靈活性,但是也會讓程序的性能有一定的損失。雖然這種損失很小,但當我們只需要定義一個很小的結構時,用類來定義就有些浪費,對於這樣的問題,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。