32

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 happier with a proper definition. Does anyone have one?

m.edmondson
  • 30,382
  • 27
  • 123
  • 206
  • Not all types that are integrated into the language are integral (System.Type, object...). Basically a type is integral if it's on the list of integral types - a tautological definition, but nonetheless true. – SWeko Mar 01 '11 at 11:57
  • 2
    @SWeko: In this context [integral](http://wordnetweb.princeton.edu/perl/webwn?s=integral) means "of or denoted by an integer", not "built-in". – configurator Mar 26 '11 at 20:49
  • @configurator: yes, and that's exactly what I'm saying in the comment. – SWeko Mar 26 '11 at 21:31
  • 1
    @SWeko: My definition wasn't tautological though. A type is integral if it is a type of integer; it is impossible to define your own integer type, therefore the types of integer are sbyte, byte, short, ushort, int, uint, long, ulong. – configurator Mar 26 '11 at 21:34
  • @Configurator: System.Numeric.BigInteger is an integer, and yet, it's not a valid switch expression. – SWeko Mar 26 '11 at 21:38
  • @SWeko: It represents an integer, but it isn't an integer itself; it's a struct with lots of stuff in it. – configurator Mar 26 '11 at 21:43

3 Answers3

31

"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.

thatWiseGuy
  • 384
  • 2
  • 3
  • 18
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 1
    How can I test for true / false in a switch statement then? It seems to be neither integral nor a string and there isn't an implicit conversion for it. – m.edmondson Mar 01 '11 at 12:00
  • @m.edmondson: I am not sure if I understand the problem. Are you trying to test whether or not a type is integral at execution time? – Andrew Hare Mar 01 '11 at 12:06
  • I'm trying to understand why I can do this: switch (true) { case false: actual = "false"; break; case true: actual = "true"; break; } – m.edmondson Mar 01 '11 at 12:18
  • 1
    @m.edmondson: Ok, I see what you mean. I guess you could say that you could chalk this up to bad documentation. MSDN should say that you can provide a boolean expression (because the compiler does allow it). I think the reason that it was omitted was because this is an edge case and not very useful (since your example would be much better expressed with an `if` statement). – Andrew Hare Mar 01 '11 at 12:24
  • @Andrew Hare - Very confusing I think and the documentation is certainly misleading. I think switch(true) does have its uses as it tends to be more readable and succinct than a load of elseif which muddy the solution – m.edmondson Mar 01 '11 at 12:30
  • @m.edmondson could you expand on what you mean by 'a load of elseif' ? A `switch` on a `bool` can only have two `case` s! – AakashM Mar 01 '11 at 18:50
  • @AakashM: I don't think this is precisely true, actually. See comment by Anders Dalvander on Eric's blog on [switch oddities](http://blogs.msdn.com/b/ericlippert/archive/2009/08/13/four-switch-oddities.aspx?PageIndex=2#comments). One hopes that there is no production code which relies on this. – Brian Mar 02 '11 at 04:14
  • @AndrewHare I understand that Int, long, short are integral type. So, what's the type of float and double? – Quazi Irfan Mar 09 '16 at 12:36
  • 2
    [This](https://www.cs.auckland.ac.nz/references/unix/digital/AQTLTBTE/DOCU_032.HTM) is a C spec, but I think it helps clear things up. An integral type is anything that is *represented* by an integer. This includes regular integer types, but also booleans (usually 1 or 0), characters (usually their integer ascii value), and sometimes string literals (array of chars so an array of integers essentially). `true` is a boolean in this case, making it an integral type. – Cole Mar 03 '21 at 05:14
  • @thatWiseGuy what is literal string? – Ibtida Bin Ahmed Sep 16 '22 at 06:06
  • I agree with @Cole said. integer types is value that **can be represent as** integer types – Elec Apr 10 '23 at 02:30
12

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.


Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
4

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

AakashM
  • 62,551
  • 17
  • 151
  • 186