Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

c# - How to specify a short int literal without casting?

Is there a way to specify that my variable is a short int? I am looking for something similar to M suffix for decimals. For decimals, I do not have to say

var d = (decimal)1.23;

I can just write as follows:

var d = 1.23M;

Is there a way to write this

   var s  = SomeLiteralWithoutCast

so that s is implied to be short int?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Short answer, No. In C#, there's no letter S that could be used as var a = 123S that would indicate that a is of type short. There's L for long, F for float, D for double, M for decimal, but not S. It would be nice if there was, but there isn't.

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

but not

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...