17、C#中的常量和結構的定義與使用
在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 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#中的常量和結構的定義與使用