C#的类型转换
2011-08-13 14:09
155 查看
在计算机中,所有的数据都是以0和1的形式存在的,变量的含义是通过数据的这种解释方式来传达的。在实际应用中很多地方要用到不同数据类型之间的转换,请看下面的一个例子
string str = null;
double num=null;
Console.WriteLine("enter a serial of numbers:\t");
str = Console.ReadLine();//读取一行数字
num = Convert.ToDouble(str);//字符串转换成数字
类型转换包括有两种方法:
隐式转换和显式转换,下面将分别介绍这两种方法。
enter a serial of numbers:
3.1415
str=3.1415
num=3.1415
一. 隐式转换
隐式转换不要做任何工作,不要编写其他的代码,可以直接转换。
请看:
如果将上面的(1)处的代码改为:ch = (char)num1;那么编译的时候就没有错误,这种方法就是显式转换。其结果如下:
从上图可以看出隐式和显式转换的一些区别了。
二、显式转换
显式转换有个很明显的标识,在要转换的数据之前有个标识符,例如:
ch = (char)num1。当然显式转换不止只有这个方法,还可以用Convert方法来处理数据的转换。
先看下几个例子,再对照后面给出的两个表格的来验证。本文发表于编程入门网:www.bianceng.cn
下面的两个表格在变量的转换过程中国比较重要,特别是Convert的各种方法
表1:左边类型可以无错误的直接成功转换为右边的类型
这些转换与两种数据的长度相关,这个表不需要记住,在实际编程过程中如果有什么错误,系统编译器会自动提醒。
表2:类Convert的常用方法:类型1 var1=Convert.method(var2);
以上16个方法中只有几个在实际编程过程中用到很多,特别是Convert.ToString();Convert.ToChar();
Convert.ToDateTime()这几个使用频率很高,大家要自己编程体会,下一节将会介绍C#中常用的3种复杂类型变量:结构体,数组和枚举类型。
string str = null;
double num=null;
Console.WriteLine("enter a serial of numbers:\t");
str = Console.ReadLine();//读取一行数字
num = Convert.ToDouble(str);//字符串转换成数字
类型转换包括有两种方法:
隐式转换和显式转换,下面将分别介绍这两种方法。
enter a serial of numbers:
3.1415
str=3.1415
num=3.1415
一. 隐式转换
隐式转换不要做任何工作,不要编写其他的代码,可以直接转换。
请看:
ushort num1; char ch='a'; num1 = ch; | ch=a numn1=97 |
num1 = 98; ch = num1;// (1) | 错误:法将类型“ushort”隐式转换为“char”。存在一个显式转换(是否缺少强制转换?) |
ushort num1; char ch='a'; num1 = ch;//直接转换 | ch=a numn1=97 |
num1 = 98; ch = (char)num1;//间接转换,num1的前面多了个char. | ch=a numn1=97 ch=b numn1=98 |
二、显式转换
显式转换有个很明显的标识,在要转换的数据之前有个标识符,例如:
ch = (char)num1。当然显式转换不止只有这个方法,还可以用Convert方法来处理数据的转换。
先看下几个例子,再对照后面给出的两个表格的来验证。本文发表于编程入门网:www.bianceng.cn
int age = 22; double myage = (double)age; //显式转换 Console.WriteLine("int age={0}", age); Console.WriteLine("double myage={0}", myage); float price = 55.55F; decimal cupon = Convert.ToDecimal(price); // 使用Convert方法来处理。 Console.WriteLine("float price={0}", price); Console.WriteLine("decimal cupon={0}", cupon); | int age=22 double myage=22 float price=55.55 decimal cupon=55.55 |
表1:左边类型可以无错误的直接成功转换为右边的类型
类型 | 编译器隐式执行的转换 |
byte | short,ushort,int,uint,long,ulong,float,double,decimal |
sbyte | short,int,long,float,double,decimal |
short | int,long,float,double.decimal |
ushort | int,uint,float,ufloat,double,decimal |
int | long,float,double,decimal |
uint | float,double,decimal, |
long | float,double,decimal |
ulong | float,double,decimal |
float | double |
char | ushort,int,uint,long,ulong,float,double,decimal |
表2:类Convert的常用方法:类型1 var1=Convert.method(var2);
Convert.ToBoolean(); | Convert.ToInt64() |
Convert.ToByte(); | Convert.ToSByte(); |
Convert.ToChar(); | Convert.ToString(); |
Convert.ToDecimal() | Convert.ToSingle(); |
Convert.ToDouble() | Convert.ToUInt16(); |
Convert.ToDateTime() | Convert.ToUInt32(); |
Convert.ToDouble(); | Convert.ToUInt64(); |
Convert.ToInt16(); | Convert.ToInt32() |
Convert.ToDateTime()这几个使用频率很高,大家要自己编程体会,下一节将会介绍C#中常用的3种复杂类型变量:结构体,数组和枚举类型。
相关文章推荐
- C#数据类型转换(1--隐式转换)
- C#中日期类型数据的转换格式
- 【Professional C# 读书笔记】(二)-----语言基础:继承,运算符,类型转换的杂七杂八
- C#引用C++ Dll 所有类型转换方法
- C#调用C++的Dll(dll,DLL)类型之间转换笔记
- C#中的类型转换
- C#中的类型转换
- c# 类型转换
- c#基础(三)----类型转换
- C# 类型转换
- C# 处理数据类型转换帮助类
- C#调用dll时的类型转换
- 用VS2008生成C++代码的dll及在C#中的调用及常见的类型转换
- C#中的自定义类型转换
- C#中的类型转换大总结
- C# 调用C++ DLL 的类型转换
- C# yyyyMMdd 类型字符串转换为datetime 类型
- C#调用VC的DLL的接口函数参数类型转换一览表_蓝竹傲雪
- C#基础总结之一变量常量-if嵌套语句-witch结构-类型转换
- C#:数据类型转换