第五章变量的更多内容

5.1类型转换
隐式转换:让编译器执行转换,任何变量a,只要其取值范围包含在类型b里面,就可以隐式转换为b
显示转换:我已经知道我这么做的后果,但是我会负责


5.2复杂的变量类型
枚举、结构、数组


5.2.1枚举
enum orientation :byte
{
north=1,
south=1,
east=1,
west=1
}
//枚举的实际值可以为其他的枚举,枚举的值可以为空,如果为空就是上一个明确声明的值大于1


5.2.2结构
结构就是由几个数据组成的数据结构,这些数据可能具有不同的类型。
enum orientation :byte
{
north=1,
south=1,
east=1,
west=1
}
struct route
{
public orientation direction;
public double distance;
}


5.2.3数组
1.声明数组
注意,数组不能像其他基本类型先声明后赋值
如:
int[] myIntArray=new int[5];
myIntArray={1,2,3,4,5}; //错误的
myIntArray[0]=1; //正确的


int[] myIntArray={5,9,10,2,99};
int[] myIntArray=new int[5]{5,9,10,2,99};


3.多维数组(矩形数组)
使用多个索引访问其元素的数组
double[,] hillHeight = new double[3, 4];
double[,] hillHeight = { { 1, 2, 3, 4 }, { 2, 3, 4, 5 }, { 3, 4, 5, 6} };
多维数组的每一个元素的长度应该一致


4.数组的数组(锯齿数组)
元素长度可以不一样,但是基本数据类型要一样
int[][] jaggedIntArray=new int[2][];
int[][] jaggedIntArray={new int[]{1,2,3},new int[]{1},new int[]{1,2}};


循环赋值
foreach (int[] IntArray in jaggedIntArray)
{
foreach(int ints in IntArray)
{
Console.WriteLine(ints);
}
}


5.3字符串的处理
string类型变量可以看作是char变量的只读数组
string myString="A string";
char myChar=myString[1];





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值