c# - 如何在不强制转换的情况下指定一个 short int 文字?

标签 c#

有没有办法指定我的变量是一个 short int? 我正在寻找类似于小数的 M 后缀的东西。对于小数,我不必说

var d = (decimal)1.23;

我可以这样写:

var d = 1.23M;

有没有办法这样写

var s = SomeLiteralWithoutCast

所以 s 隐含为 short int?

最佳答案

简短回答,。在 C# 中,没有可用作 var a = 123S 的字母 S 表示 a 的类型为 shortL 代表longF 代表floatD 代表double M 代表十进制但不是S。如果有就好了,但是没有。

var a = 1M;  // decimal
var a = 1L;  // long
var a = 1F;  // float
var a = 1D;  // double
var a = 1;   // int

var a = 1U;  // uint
var a = 1UL; // ulong

但不是

var a = 1S; // not possible, you must use (short)1;

关于c# - 如何在不强制转换的情况下指定一个 short int 文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8670511/

相关文章:

c# - 等待页面加载

c# - 在 SQL CLR 中访问 COM

c# - 最佳实践 : Returning from method in case block of switch statement?

c# - 如何捕获 EF 异常

C# 如何将 short[] 转换为 bool[]?

c# - 在 C# Telegram Bots 中使用键盘编辑短信

c# - 使用 Wrapper 对象正确清理 excel interop 对象

c# - 如何在iTextSharp中引入上标?

c# - 如何序列化dll中的对象?

c# - 循环引用对象的垃圾回收