当前位置: 移动技术网 > IT编程>开发语言>c# > C#.net中的类型转换详细介绍

C#.net中的类型转换详细介绍

2019年07月18日  | 移动技术网IT编程  | 我要评论
最近由于编程的需要,对 c# 的类型转换做了一些研究,其内容涉及 c# 的装箱/拆箱/别名、数值类型间相互转换、字符的 ascii 码和 unicode 码、数值字符串和数

最近由于编程的需要,对 c# 的类型转换做了一些研究,其内容涉及 c# 的装箱/拆箱/别名、数值类型间相互转换、字符的 ascii 码和 unicode 码、数值字符串和数值之间的转换、字符串和字符数组/字节数组之间的转换、各种数值类型和字节数组之间的转换、十六进制数输出以及日期型数据的一些转换处理,在这里与大家分享。

1. 装箱、拆箱还是别名

许多 c#.net 的书上都有介绍 int -> int32 是一个装箱的过程,反之则是拆箱的过程。许多其它变量类型也是如此,如:short <-> int16,long <-> int64 等。对于一般的程序员来说,大可不必去了解这一过程,因为这些装箱和拆箱的动作都是可以自动完成的,不需要写代码进行干预。但是我们需要记住这些类型之间的关系,所以,我们使用“别名”来记忆它们之间的关系。

c# 是全面向对象的语言,比 java 的面向对象都还彻底——它把简单数据类型通过默认的装箱动作封装成了类。int32、int16、int64 等就是相应的类名,而那些我们熟悉的、简单易记的名称,如 int、short、long 等,我们就可以把它称作是 int32、int16、int64 等类型的别名。

那么除了这三种类型之外,还有哪些类有“别名”呢?常用的有如下一些:

bool -> system.boolean (布尔型,其值为 true 或者 false)
char -> system.char (字符型,占有两个字节,表示 1 个 unicode 字符)
byte -> system.byte (字节型,占 1 字节,表示 8 位正整数,范围 0 ~ 255)
sbyte -> system.sbyte (带符号字节型,占 1 字节,表示 8 位整数,范围 -128 ~ 127)
ushort -> system.uint16 (无符号短整型,占 2 字节,表示 16 位正整数,范围 0 ~ 65,535)
uint -> system.uint32 (无符号整型,占 4 字节,表示 32 位正整数,范围 0 ~ 4,294,967,295)
ulong -> system.uint64 (无符号长整型,占 8 字节,表示 64 位正整数,范围 0 ~ 大约 10 的 20 次方)
short -> system.int16 (短整型,占 2 字节,表示 16 位整数,范围 -32,768 ~ 32,767)
int -> system.int32 (整型,占 4 字节,表示 32 位整数,范围 -2,147,483,648 到 2,147,483,647)
long -> system.int64 (长整型,占 8 字节,表示 64 位整数,范围大约 -(10 的 19) 次方 到 10 的 19 次方)
float -> system.single (单精度浮点型,占 4 个字节)
double -> system.double (双精度浮点型,占 8 个字节)
我们可以用下列代码做一个实验:

复制代码 代码如下:

private void testalias() {  
// this.textbox1 是一个文本框,类型为 system.windows.forms.textbox  
// 设计中已经将其 multiline 属性设置为 true  
byte a = 1; char b = 'a'; short c = 1;  
int d = 2; long e = 3; uint f = 4; bool g = true;  
this.textbox1.text = "";  
this.textbox1.appendtext("byte -> " + a.gettype().fullname + "\n");  
this.textbox1.appendtext("char -> " + b.gettype().fullname + "\n");  
this.textbox1.appendtext("short -> " + c.gettype().fullname + "\n");  
this.textbox1.appendtext("int -> " + d.gettype().fullname + "\n");  
this.textbox1.appendtext("long -> " + e.gettype().fullname + "\n");  
this.textbox1.appendtext("uint -> " + f.gettype().fullname + "\n");  
this.textbox1.appendtext("bool -> " + g.gettype().fullname + "\n");  
}

在窗体中新建一个按钮,并在它的单击事件中调用该 testalias() 函数,我们将看到运行结果如下:
复制代码 代码如下:

byte -> system.byte  
char -> system.char  
short -> system.int16 
int -> system.int32 
long -> system.int64 
uint -> system.uint32 
bool -> system.boolean

这足以说明各别名对应的类!

2. 数值类型之间的相互转换

这里所说的数值类型包括 byte, short, int, long, fload, double 等,根据这个排列顺序,各种类型的值依次可以向后自动进行转换。举个例来说,把一个 short 型的数据赋值给一个 int 型的变量,short 值会自动行转换成 int 型值,再赋给 int 型变量。如下例:

复制代码 代码如下:

private void testbasic() {  
byte a = 1; short b = a; int c = b;  
long d = c; float e = d; double f = e;  
this.textbox1.text = "";  
this.textbox1.appendtext("byte a = " + a.tostring() + "\n");  
this.textbox1.appendtext("short b = " + b.tostring() + "\n");  
this.textbox1.appendtext("int c = " + c.tostring() + "\n");  
this.textbox1.appendtext("long d = " + d.tostring() + "\n");  
this.textbox1.appendtext("float e = " + e.tostring() + "\n");  
this.textbox1.appendtext("double f = " + f.tostring() + "\n");  
}

译顺利通过,运行结果是各变量的值均为 1;当然,它们的类型分别还是 system.byte 型……system.double 型。现在我们来试试,如果把赋值的顺序反过来会怎么样呢?在 testbasic() 函数中追加如下语句:
复制代码 代码如下:

int g = 1;  
short h = g;  
this.textbox1.appendtext("h = " + h.tostring() + "\n");

结果编译报错:

g:\projects\visual c#\convert\form1.cs(118): 无法将类型“int”隐式转换为“short”

其中,form1.cs 的 118 行即 short h = g 所在行。

这个时候,如果我们坚持要进行转换,就应该使用强制类型转换,这在 c 语言中常有提及,就是使用“(类型名) 变量名”形式的语句来对数据进行强制转换。如上例修改如下:

复制代码 代码如下:

short g = 1;  
byte h = (byte) g; // 将 short 型的 g 的值强制转换成 short 型后再赋给变量 h  
this.textbox1.appendtext("h = " + h.tostring() + "\n");

编译通过,运行结果输出了 h = 1,转换成功。

但是,如果我们使用强制转换,就不得不再考虑一个问题:short 型的范围是 -32768 ~ 23767,而 byte 型的范围是 0 ~ 255,那么,如果变量 g 的大小超过了 byte 型的范围又会出现什么样的情况呢?我们不妨再一次改写代码,将值改为 265,比 255 大 10

复制代码 代码如下:

short g = 265; //265 = 255 + 10  
byte h = (byte) g;  
this.textbox1.appendtext("h = " + h.tostring() + "\n");

编译没有出错,运行结果却不是 h = 265,而是 h = 9。

因此,我们在进行转换的时候,应当注意被转换的数据不能超出目标类型的范围。这不仅体现在多字节数据类型(相对,如上例的 short) 转换为少字节类型(相对,如上例的 byte) 时,也体现在字节数相同的有符号类型和无符号类型之间,如将 byte 的 129 转换为 sbyte 就会溢出。这方面的例子大同小异,就不详细说明了。

3. 字符的 ascii 码和 unicode 码

很多时候我们需要得到一个英文字符的 ascii 码,或者一个汉字字符的 unicode 码,或者从相关的编码查询它是哪一个字符的编码。很多人,尤其是从 vb 程序序转过来学 c# 的人,会报怨 c# 里为什么没有提供现成的函数来做这个事情——因为在 vb 中有 asc() 函数和 chr() 函数用于这类转换。

但是如果你学过 c,你就会清楚,我们只需要将英文字符型数据强制转换成合适的数值型数据,就可以得到相应的 ascii 码;反之,如果将一个合适的数值型数据强制转换成字符型数据,就可以得到相应的字符。

c# 中字符的范围扩大了,不仅包含了单字节字符,也可以包含双字节字符,如中文字符等。而在字符和编码之间的转换,则仍延用了 c 语言的做法——强制转换。不妨看看下面的例子

复制代码 代码如下:

private void testchar() {  
char ch = 'a'; short ii = 65;  
this.textbox1.text = "";  
this.textbox1.appendtext("the ascii code of \'" + ch + "\' is: " + (short) ch + "\n");  
this.textbox1.appendtext("ascii is " + ii.tostring() + ", the char is: " + (char) ii + "\n");  
char cn = '中'; short uc = 22478;  
this.textbox1.appendtext("the unicode of \'" + cn + "\' is: " + (short) cn + "\n");  
this.textbox1.appendtext("unicode is " + uc.tostring() + ", the char is: " + (char) uc + "\n");  
}

它的运行结果是
复制代码 代码如下:

the ascii code of 'a' is: 97 
ascii is 65, the char is: a  
the unicode of '中' is: 20013 
unicode is 22478, the char is: 城

从这个例子中,我们便能非常清楚的了解——通过强制转换,可以得以字符的编码,或者得到编码表示的字符。如果你需要的不是 short 型的编码,请参考第 1 条进行转换,即可得到 int 等类型的编码值。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网