手机钓鱼游戏中文版:“byte”是什么单位

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/10 14:11:32

C# 程序员参考

byte
byte 关键字代表一种整型,该类型按下表所示存储值:

类型 范围 大小 .NET Framework 类型
byte 0 到 255 无符号 8 位整数 System.Byte

文本
可如下例所示声明并初始化 byte 变量:

byte myByte = 255;
在以上声明中,整数 255 从 int 类型隐式转换为 byte 类型。如果整数超出了 byte 类型的范围,则将发生编译错误。

转换
存在从 byte 类型到 short、ushort、int、uint、long、ulong、float、double 或 decimal 类型的预定义隐式转换。

不能将存储大小更大的非文字数字类型隐式转换为 byte 类型(请参见整型表中有关整型存储大小的信息)。例如,请看以下两个 byte 变量 x 和 y:

byte x = 10, y = 20;
以下赋值语句将产生一个编译错误,原因是赋值运算符右侧的算术表达式默认情况下计算为 int。

byte z = x + y; // Error: conversion from int to byte
若要解决此问题,请使用强制转换:

byte z = (byte)(x + y); // OK: explicit conversion
但是,在目标变量具有相同或更大的存储大小时,使用下列语句是可能的:

int x = 10, y = 20;
int m = x + y;
long n = x + y;
同样,不存在浮点型到 byte 类型的隐式转换。例如,除非使用显式强制转换,否则以下语句将生成一个编译器错误:

byte x = 3.0; // Error: no implicit conversion from double
byte y = (byte)3.0; // OK: explicit conversion
调用重载方法时,必须使用显式转换。例如,请看以下使用 byte 和 int 类型参数的重载方法:

public static void MyMethod(int i) {}
public static void MyMethod(byte b) {}
使用 byte 显式转换可保证调用正确的类型,例如:

MyMethod(5); // Calling the method with the int parameter
MyMethod((byte)5); // Calling the method with the byte parameter
有关兼用浮点型和整型的算术表达式的信息,请参见 float 和 double。

有关隐式数值转换规则的更多信息,请参见隐式数值转换表。

你可以参考这个网页
http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/csref/html/vclrfByte_PG.asp

字节