请单击以进行评分并提供反馈
MSDN
MSDN Library
Visual C#
C# 参考
C# 关键字
类型
值类型
 short

  开启低带宽视图
此页面仅适用于
Microsoft Visual Studio 2005/.NET Framework 2.0

同时提供下列产品的其他版本:
C# 语言参考
short(C# 参考)

short 关键字表示一种整数数据类型,该类型根据下表显示的大小和范围存储值。

类型 范围 大小 .NET Framework 类型

short

-32,768 到 32,767

有符号 16 位整数

System.Int16

可如下例所示声明并初始化 short 类型的变量:

          short x = 32767;

在以上声明中,整数 32767int 隐式转换为 short。如果整数的长度超过了 short 存储位置的大小,则将产生编译错误。

调用重载方法时必须使用强制转换。以下面使用 shortint 参数的重载方法为例:

public static void SampleMethod(int i) {}
public static void SampleMethod(short s) {}

使用 short 强制转换可保证调用正确的类型,例如:

SampleMethod(5);         // Calling the method with the int parameter
SampleMethod((short)5);  // Calling the method with the short parameter

存在从 shortintlongfloatdoubledecimal 的预定义隐式转换。

不能将存储大小更大的非文本数值类型隐式转换为 short(有关整型的存储大小的信息,请参见整型表(C# 参考))。例如,请看以下两个 short 变量 xy

          short x = 5, y = 12;

以下赋值语句将产生一个编译错误,原因是赋值运算符右侧的算术表达式在默认情况下的计算结果为 int 类型。

short z = x + y; // Error: no conversion from int to short

若要解决此问题,请使用强制转换:

short z = ( short )(x + y); // OK: explicit conversion

但是,在目标变量具有相同或更大的存储大小时,使用下列语句是可能的:

int m = x + y;
long n = x + y;

不存在从浮点型到 short 的隐式转换。例如,除非使用显式强制转换,否则以下语句将生成一个编译器错误:

          short x = 3.0;          // Error: no implicit conversion from double
short y = (short)3.0;   // OK: explicit conversion

有关兼用浮点型和整型的算术表达式的信息,请参见 floatdouble

有关隐式数值转换规则的更多信息,请参见隐式数值转换表(C# 参考)

有关更多信息,请参见 C# 语言规范中的以下各章节:

  • 1.3 类型和变量

  • 4.1.5 整型

社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2009 Microsoft Corporation 版权所有。 保留所有权利  |  商标  |  隐私权声明
Page view tracker