C# 笔记 变量与类型转换(基础+总结)

文章虽然浅显,适合初学者,主要在总结的同时方便自己查阅。

简单的 隐式转换和显式转换就不解释了

下列是整型类型的内存字节数:
int 4字节内存使用   -2147483648~2147483647
uint 不带负号的整数 0-4294967295

short  2字节 -32768~32767
ushort 2字节 0-65535

long  8字节 -9223372036854775808~9223372036854775807
ulong 8字节
 
sbyte   1字节   -128-127
Byte    1字节   0-255

char    2个字节  表示1个字符

float   4字节  7位精度
double  8字节 15-16精度
decimal 16字节 28位精度


注意:对于ushort 与char是可以相互转换的,因为char也是单字符的整型,存储的是一个数值,所以是数值类型。
案例1:
    

static void Main(string[] args)
{
ushort shortnum;
char charnum ='a';
shortnum = charnum;
Console.WriteLine(charnum);//显示a
Console.WriteLine(shortnum);//显示97
}



即使存储的信息一样,但是表示的方式不一样的。

注:bool 和string 没有隐式转换!彼此之间没有关系的类型不能进行强制转换。


溢出检查环境:(用于强制转换)
案例2:
    

static void Main(string[] args)
{
ushort shortnum=281;
byte bytenum;
bytenum = (byte)shortnum;
Console.WriteLine(bytenum);//显示25,因为数据溢出了
Console.WriteLine(shortnum);//显示281

//使用checked检测是否溢出
bytenum = checked((byte)shortnum);//未处理的异常: System.OverflowException: 算术运算导致溢出。//unchecked就是不检测是否溢出

}





Convert显式转换:
注:对于Convert转换是肯定要检测数据溢出的
       

        Convert.ToByte();//转换Byte
Convert.ToChar();
Convert.ToInt16();//转换short
Convert.ToInt32();//转换int
Convert.ToInt64();//转换long
Convert.ToSByte();//转换sbyte
Convert.ToUInt16();//转换ushort
Convert.ToUInt32();//转换uint
Convert.ToUInt64();//转换ulong
Convert.ToBoolean();//转换bool
Convert.ToSingle();//转换float
Convert.ToDouble();
Convert.ToDecimal();





案例3:
   

static void Main(string[] args)
{
ushort shortnum=281;
byte bytenum;
bytenum = Convert.ToByte(shortnum);//对于Convert转换是肯定要检测数据溢出的
//未处理的异常: System.OverflowException: 值对于无符号的字节太大或太小。
Console.WriteLine(bytenum);//显示25,因为数据溢出了
Console.WriteLine(shortnum);//显示281
}



注:short*float是需要进行转换的,默认是把short转换成float,因为short是2字节,float是4字节-隐式转换


复杂的变量类型:
1.枚举

test1:
public class EnumTest
{
enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };

static void Main()
{
int x = (int)Days.Sun;
int y = (int)Days.Fri;
Console.WriteLine("Sun = {0}", x);
Console.WriteLine("Fri = {0}", y);
}
}
/* Output:
Sun = 0
Fri = 5
*/



注意:enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri}; 强制序列号从1开始而不是0.enum的默认类型是Int.但是不可以是char类型

test2:
public class EnumTest2
{
enum Range : long { Max = 2147483648L, Min = 255L };
static void Main()
{
long x = (long)Range.Max;
long y = (long)Range.Min;
Console.WriteLine("Max = {0}", x);
Console.WriteLine("Min = {0}", y);
}
}
/* Output:
Max = 2147483648
Min = 255
*/




test3:
public enum CarOptions
{
// The flag for SunRoof is 0001.
SunRoof = 0x01,
// The flag for Spoiler is 0010.
Spoiler = 0x02,
// The flag for FogLights is 0100.
FogLights = 0x04,
// The flag for TintedWindows is 1000.
TintedWindows = 0x08,
}

class FlagTest
{
static void Main()
{
// The bitwise OR of 0001 and 0100 is 0101.
CarOptions options = CarOptions.SunRoof | CarOptions.FogLights;

// Because the Flags attribute is specified, Console.WriteLine displays
// the name of each enum element that corresponds to a flag that has
// the value 1 in variable options.
Console.WriteLine(options);
// The integer value of 0101 is 5.
Console.WriteLine((int)options);
}
}
/* Output:
SunRoof, FogLights
5
*/






2.结构
结构类型可能是多个变量,但是数据类型不一样。
 

public struct Book
{
public decimal price;
public string title;
public string author;
}



案例1:
   

enum ori:byte  //enum默认是int类型
{
north = 1,
south = 2,
east = 3,
west = 4
}
struct routemap //结构类型,用方位和精度表示路由
{
public double distance;
public ori direction;

}
static void Main()
{
routemap map;//声明结构类型的变量
ori ori1=ori.east;//声明枚举的变量并赋值

map.direction = ori.east;
map.distance = 17.5;
Console.WriteLine(ori.east);//直接调用枚举
Console.WriteLine((byte)ori.east);//转换枚举类型的值
Console.WriteLine(ori1);
Console.WriteLine((byte)ori1);

Console.WriteLine(map.distance+" "+map.direction);
}




案例2:

public class EnumTest
{
enum ori:byte //enum默认是int类型
{
north = 1,
south = 2,
east = 3,
west = 4
}
struct routemap //结构类型,用方位和精度表示路由
{
public double distance;
public ori direction;

}
static void Main()
{
routemap map;//声明结构类型的变量
ori ori1=ori.east;//声明枚举的变量并赋值
int i = 4;

map.direction = (ori)i;//让值变为枚举
map.distance = 17.5;

Console.WriteLine(ori.east);//直接调用枚举
Console.WriteLine((byte)ori.east);//转换枚举类型的值
Console.WriteLine(ori1);
Console.WriteLine((byte)ori1);

Console.WriteLine(map.distance+" "+map.direction);
}
}





posted @ 2011-12-02 16:48  Anleb  阅读(3338)  评论(1编辑  收藏  举报