开发者

What exactly are Integral Types?

开发者 https://www.devze.com 2023-02-14 06:36 出处:网络
Having studied the switch documentation and discovering it can only switch on integral types I set about looking for a definition.I can\'t find one anywhere. I can only find a list of integral types.

Having studied the switch documentation and discovering it can only switch on integral types I set about looking for a definition. I can't find one anywhere. I can only find a list of integral types.

I could take a guess that integral types are the types which are integrated into the language, however I'd be ha开发者_StackOverflow社区ppier with a proper definition. Does anyone have one?


"Integral" refers to integer types (i.e. whole numbers). In C# this means types like int, long, short, etc.

Please see Integral Types Table (C# Reference):

The following table shows the sizes and ranges of the integral types, which constitute a subset of simple types.

Edit: Keep in mind that the switch statement supports literal strings as well.


The documentation you are studying was written in 2003 and is not up-to-date for the latest version of the language. I suggest that you stop studying the archive of the 2003 documentation and instead read the 2010 documentation if you are using a more modern version of C#.

The definitive reference that answers your question is the C# specification section 8.7.2, a portion of which I reproduce for your convenience here.


The governing type of a switch statement is established by the switch expression.

• If the type of the switch expression is sbyte, byte, short, ushort, int, uint, long, ulong, bool, char, string, or an enum-type, or if it is the nullable type corresponding to one of these types, then that is the governing type of the switch statement.

• Otherwise, exactly one user-defined implicit conversion must exist from the type of the switch expression to one of the following possible governing types: sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or, a nullable type corresponding to one of those types.

• Otherwise, if no such implicit conversion exists, or if more than one such implicit conversion exists, a compile-time error occurs.



The sense of 'integral' being used here is the one in section 1 subsection b sub-sub-section 1 (!) at http://www.merriam-webster.com/dictionary/integral :

being, containing, or relating to one or more mathematical integers

0

精彩评论

暂无评论...
验证码 换一张
取 消