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];
隐式转换:让编译器执行转换,任何变量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];