5.1 类型转换
1.隐式转换
任何类型A,只有其取值范围完全包含在类型B的取值范围内,就可以隐式转换为类型B。
2.使用Convert命令进行显示转换
Convert.ToBoolean(val)、Convert.ToByte(val)、Convert.ToChar(val)、Convert.ToDecimal(val)
Convert.ToDouble(val)、Convert.ToInt16(val)、Convert.ToInt32(val)、Convert.ToInt64(val)
Convert.ToSByte(val)、Convert.ToSingle(val)、Convert.ToUInt16(val)、Convert.ToUInt32(val)
Convert.ToUInt64(val)
val可以是各种类型的变量。
5.2 复杂的变量类型
1.枚举
enum typeName
{
value1,
value2,
value3,
……
valueN
}
typeName varName;
varName = typeName.value;
枚举使用一个基本类型来存储。枚举类型可以提取的每个值都存储为该基本类型的一个值,
在默认的情况下,该类型为int。
在枚举声明中添加类型,就可以指定其他基本类型:
enum typeName :underlyingType
{
value1,
value2,
value3,
……
valueN
}
枚举的基本类型可以是byte,sbyte,short,ushort,int,uint,long和ulong。
总结
1.1枚举的定义
enum Fabric{Cotton = 1,Silk = 2,Wool = 4,Rayon = 8,Other = 128}
1.2符号名和常数值的互相转换:
Fabric fab = Fabric.Cotton;
int fabNum = (int)fab;//转换为常数值。必须使用强制转换。
Fabric fabString = (Fabric)1;//常数值转换成符号名。如果使用ToString(),则是((Fabric)1).ToString(),注意必须有括号。
string fabType = fab.ToString();//显示符号名
string fabVal = fab.ToString ("D");//显示常数值
1.3获得所有符号名的方法:
foreach (string s in Enum.GetNames(typeof(MyFamily)))
{
Console.WriteLine(s);
}
1.4枚举作为函数参数。经常和switch结合起来使用:
public static double GetPrice(Fabric fab)
{
switch (fab)
{
case Fabric.Cotton: return (3.55);
case Fabric.Silk: return (5.65);
case Fabric.Wool: return (4.05);
case Fabric.Rayon: return (3.20);
case Fabric.Other: return (2.50);
default: return (0.0);
}
}
1.5Enum类的使用:
Enum.IsDefinde、Enum.Parse两种方法经常一起使用,来确定一个值或符号是否是一个枚举的成员,
然后创建一个实例。
Enum.GetName打印出一个成员的值;Enum.GetNames打印出所有成员的值。
其中注意typeof的使用。这一点很重要。
public enum MyFamily
{
YANGZHIPING = 1,
GUANGUIQIN = 2,
YANGHAORAN = 4,
LIWEI = 8,
GUANGUIZHI = 16,
LISIWEN = 32,
LISIHUA = 64,
}
string s = "YANGHAORAN";
if (Enum.IsDefined(typeof(MyFamily), s))
{
MyFamily f = (MyFamily)Enum.Parse(typeof(MyFamily), s);
GetMyFamily(f);
Console.WriteLine("The name is:" + Enum. GetName(typeof(MyFamily), 2));
string[] sa = Enum.GetNames(typeof(MyFamily));
foreach (string ss in sa)
{
Console.WriteLine(ss);
}
}
2.结构
结构就是由几个数据组成的数据结构,这些数据可能有不同的类型。
定义结构
struct <typeName>
{
<memberDeclarations>
}
<memberDeclarations>部分包含变量的定义(称为结构的数据成员)
每个成员的声明都采用
<accessibility><type><name>;
例如:
struct route
{
public orientation direction;//枚举型变量
public double distance;
}
route myRoute;
myRoute.orientation = orientation.north;
myRoute.distance = 2.5;
补充:
1)结构具有以下特点:
1.1结构是值类型,而类是引用类型。
1.2与类不同,结构的实例化可以不使用 new 运算符。
1.3结构可以声明构造函数,但它们必须带参数。
1.4一个结构不能从另一个结构或类继承,而且不能作为一个类的基。
所有结构都直接继承自 System.ValueType,后者继承自 System.Object。
1.5结构可以实现接口。
2)结构的使用
public struct CoOrds
{
public int x, y;
public CoOrds(int p1, int p2)
{
x = p1;
y = p2;
}
}
如果使用 new 运算符创建结构对象,则会创建该结构对象,并调用适当的构造函数。
与类不同,结构的实例化可以不使用 new 运算符。
如果不使用 new,则在初始化所有字段之前,字段都保持未赋值状态且对象不可用。
//使用new关键字
class TestCoOrds
{
static void Main()
{
// Initialize:
CoOrds coords1 = new CoOrds();
CoOrds coords2 = new CoOrds(10, 10);
// Display results:
System.Console.Write("CoOrds 1: ");
System.Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);
System.Console.Write("CoOrds 2: ");
System.Console.WriteLine("x = {0}, y = {1}", coords2.x, coords2.y);
}
}
输出结果:CoOrds 1: x = 0, y = 0
CoOrds 2: x = 10, y = 10
//不使用new
class TestCoOrdsNoNew
{
static void Main()
{
// Declare an object:
CoOrds coords1;
// Initialize:
coords1.x = 10;
coords1.y = 20;
// Display results:
System.Console.Write("CoOrds 1: ");
System.Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);
}
}
输出结果:CoOrds 1: x = 10, y = 20
3.数组
3.1声明
int [] myIntArray = {1,2,3};
int [] myIntArray = new int [5];
3.2初始化
int [] myIntArray = new int [5]{1,2,3,4,5};
这种方式,数组大小必须与元素个数相匹配。
3.3foreach
foreach(<baseType><name> in <array>)
{
}
3.4多维数组
二维数组的声明
<baseType>[,]<name>;
多维数组只需多个逗号
<baseType>[,,]<name>;
初始化
double[,] hillHeight = new double [3,4];
double[,] hillHeight = {{1,2,3,4},{2,3,4,5},{3,4,5,6}};
5.3 字符串处理
1.<string>.Split()把string转换维stirng数组,把它在指定的位置分割开。
浙公网安备 33010602011771号