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 所在行。
的转换、十六进制数输出以及日期型数据的一些转换处理... byte -> System.Byte char -> System.Char ... float e = d; double f = e; this.textBox1...
是系统默认的转换, 其本质是小存储容量数据类型自动转换为大存储容量 数据类型。...从 double 到 sbyte,byte,short,ushort,int,uint,long,ulong,char,float,或 ...
C#数据类型转换方法_计算机软件及应用_IT/计算机_专业资料。C#数据类型转换方法...decimal ulong float,double,decimal float double char ushort,int,uint,long,...
为布尔型boolean;字符型char;整型byte、short、int、long;浮点型float、double。...在进行简单数据类型之间的转换(自动转换或强制转换)时,我们总是可以利用包装类...
(byte); 引用类型)的值转换为 int 类 public static int ToInt32(char); ...C# 也不例外,其基本数据类型有 int,short,long,float,double,string 等。数据...
double 型之间的转换,整数和 String 类型之间的转换,以及处理、显 示时间方面的...(byte,short,char)--int--long--float--double[/center] 简单数据类型之间的...
int,char long ulong float double decimal byte,sbyte,short,ushort,int,uint,...显示转换,都只 是从一种数值类型(包括char类型)转换到 另一种数值类型的转换...
float -> System.Single (单精度浮点型,占 4 个字节) double -> System....byte a = 1; char b = 'a'; short c = 1; int d = 2; long e ...
( byte 、 short 、 char )——int——long——float——double PS:此处的“...包装数据类型与基本数据类型之间的转换简单类型的变量转换为相应的包装类,可以利用...
false char 数据类型有:unicode 字符,16 位 对应的类类型:Integer,Float,Boolean,Character,Double,Short,Byte, Long 转换原则 从低精度向高精度转换 byte ,short...
热门文档