C#結構的學習

來源:互聯網
上載者:User

結構是程式員定義的資料類型,非常的類似於類。有資料成員和函數成員。

但是也有區別:

1)類是參考型別,而結構是實值型別;

2)結構是隱式密封的,不能被派生;

文法聲明與類相似:

//結構的聲明        struct StructName        {            //包含的成員變數        }

看下邊的範例程式碼來示範C#結構的使用方法:

static void Main(string[] args)        {            Point first, second, third;            first.x = 10; first.y = 10;            second.x = 20; second.y = 20;            third.x = first.x + second.x;            third.y = first.y + second.y;            Console.WriteLine("first:{0},{1}",first.x,first.y);            Console.WriteLine("second:{0},{1}",second.x,second.y);            Console.WriteLine("third:{0},{1}",third.x,third.y);            Console.ReadKey();        }        struct Point        {            public int x;            public int y;        }

結構是實值型別

 

1)結構類型的變數不能使null;

2)兩個結構變數不能引用同一對象

static void Main(string[] args)        {            CSimple cs1 = new CSimple(),cs2=null;            Simple ss1 = new Simple(),ss2=new Simple();            cs1.x = ss1.x = 5;            cs1.y = ss1.y = 10;            cs2 = cs1;   //賦值類執行個體            ss2 = ss1;   //賦值結構執行個體        }        class CSimple        {            public int x;            public int y;        }        struct Simple        {            public int x;            public int y;        }

先建立一個CSimple類,和一個Simple結構

在Main()分別對它們執行個體化聲明兩個個變數後,cs1和cs2分別指向在堆中的引用,而ss1和ss2分別在棧中分配空間並儲存。
把一個結構賦值給另外一個結構,就是從一個結構中把值複值給另外一個結構。與類不同的是,複製類變數時只有引用被複製。

如上面的代碼,類賦值結束後,cs2和cs1指向堆中的同一個對象。但是在結構賦值結束後,ss2成員的值和ss1成員的值相同。

 

結構中的建構函式和解構函式

語言隱式的為每個結構提供一個無參數的建構函式。這個建構函式把結構的每個成員設定為該類型的預設值,引用成員被設定成null

預定義的無參數建構函式對每個結構都存在,而且不能刪除或者重新定義。但是可以建立另外的建構函式,只要他們有參數。這和類不同,對於類,編譯器只在沒有其它建構函式聲明時提供隱式的無參數建構函式。

要調用一個建構函式,包括隱式的無參數建構函式,要使用new運算子。即使不從堆中分配記憶體也使用new運算子。

如下面的執行個體:

static void Main(string[] args)        {            Simple s1 = new Simple();  //調用隱式的建構函式            Simple s2 = new Simple(5, 10);  //調用建構函式            Console.WriteLine("{0},{1}",s1.x,s1.y);            Console.WriteLine("{0},{1}", s2.x, s2.y);                        Console.ReadKey();        }        struct Simple        {            public int x;            public int y;            public Simple(int a, int b)            {                x = a;                y = b;            }        }

也可以不適用new運算子建立結構的執行個體。但是,有一些限制:

 

1)不能使用資料成員的值,直到顯示的設定它

2)不能調用任何函數成員,直到所有資料成員已經被賦值

static void Main(string[] args)        {            Simple s1, s2;            Console.WriteLine("{0},{1}", s1.x, s1.y);//編譯錯誤,s1.x, s1.y還沒有被賦值            s2.x = 50;            s2.y = 10;            Console.WriteLine("{0},{1}", s2.x, s2.y);                  Console.ReadKey();        }        struct Simple        {            public int x;            public int y;        }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.