17、C#中的常量和結構的定義與使用

來源:互聯網
上載者:User

標籤:c3

在C#裡面,常量指的是固定不變的量。這個常量可以是數值型、文本型或布爾型。

例如:

       //定義常量
            const int AGE=16;
            const string NAME="小紅";
            const bool FLAG=true;
            
            //試圖修改常量,但是這是不可行的。
            AGE=18;
            NAME="小蘭";
            FLAG=false;

解釋:常量的定義,就是在定義變數的基礎上,在左邊增加了一個關鍵詞const。


在C#裡面,結構的定義其實就是自訂一個資料類型。怎麼說呢,就是和易語言裡面的自訂資料類型非常的相似。

結構的定義大致如下:

public struct 自訂的資料類型

{

public 類型 變數名;

public 類型 變數名;

}


比如定義一個類型為:書。

public struct type_book

{

public string title;

public string price;

public string author;

}


定義一個類型為:學生。

public struct type_student

{

public string name;

public string age;

public string gender;

public string great;

}
 

執行個體:定義一個類型為:人。並且進行運用。

            public struct type_person
            {
            public string name;
            public string age;
            public string gender;
            }

//使用這個定義的結構類型。

            type_person xiaohong;
            Console.Write("請輸入姓名:");
            xiaohong.name=Console.ReadLine();
            Console.Write("請輸入年齡:");
            xiaohong.age=Console.ReadLine();
            Console.Write("請輸入性別:");
            xiaohong.gender=Console.ReadLine();
            Console.WriteLine("姓名:{0};年齡:{1};性別:{2}",xiaohong.name,xiaohong.age,xiaohong.gender);


本文出自 “奕奕微笑” 部落格,請務必保留此出處http://yiyiweixiao.blog.51cto.com/2476874/1970429

17、C#中的常量和結構的定義與使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.