文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > IT/计算机 > 计算机软件及应用 > c#数据类型转换,BYTE,float,double,char类型间的转换方法
侵权投诉

c#数据类型转换,BYTE,float,double,char类型间的转换方法

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 所在行。

上一页第2页

热门文档

相关文档

  • C#中数据类型转换介绍

    的转换、十六进制数输出以及日期型数据的一些转换处理... byte -> System.Byte char -> System.Char ... float e = d; double f = e; this.textBox1...

  • C#的隐式和显示类型转换

    是系统默认的转换, 其本质是小存储容量数据类型自动转换为大存储容量 数据类型。...从 double 到 sbyte,byte,short,ushort,int,uint,long,ulong,char,float,或 ...

  • C#数据类型转换方法

    C#数据类型转换方法_计算机软件及应用_IT/计算机_专业资料。C#数据类型转换方法...decimal ulong float,double,decimal float double char ushort,int,uint,long,...

  • java 基本数据类型之间的转换

    为布尔型boolean;字符型char;整型byte、short、int、long;浮点型float、double。...在进行简单数据类型之间的转换(自动转换或强制转换)时,我们总是可以利用包装类...

  • C#强制类型转换

    (byte); 引用类型)的值转换为 int 类 public static int ToInt32(char); ...C# 也不例外,其基本数据类型有 int,short,long,float,double,string 等。数据...

  • JAVA的数据类型之间转换

    double 型之间的转换,整数和 String 类型之间的转换,以及处理、显 示时间方面的...(byte,short,char)--int--long--float--double[/center] 简单数据类型之间的...

  • c#- 类型转换

    int,char long ulong float double decimal byte,sbyte,short,ushort,int,uint,...显示转换,都只 是从一种数值类型(包括char类型)转换到 另一种数值类型的转换...

  • C#中的数据类型及其相互转换

    float -> System.Single (单精度浮点型,占 4 个字节) double -> System....byte a = 1; char b = 'a'; short c = 1; int d = 2; long e ...

  • Java中数据类型及其之间的转换

    ( byte 、 short 、 char )——int——long——float——double PS:此处的“...包装数据类型与基本数据类型之间的转换简单类型的变量转换为相应的包装类,可以利用...

  • java中数据类型转换

    false char 数据类型有:unicode 字符,16 位 对应的类类型:Integer,Float,Boolean,Character,Double,Short,Byte, Long 转换原则 从低精度向高精度转换 byte ,short...

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email