Just because something can be evaluated at compile time does not mean that someone has programmed a compiler for this. string.Format("{0:N2}", Math.PI) is another example.
The only way to get a counter of the number of Enum values ββis via reflection ( Enum.GetNames or something similar). Thus, this is not a constant expression, although technically the compiler could simply evaluate the expression at compile time and determine which result.
nameof is a great example. It is persistent at compile time, but there was no mechanism for extracting the result at compile time until someone developed, developed, tested, documented, and sent this function. None of them is free, and therefore the idea should compete for valuable resources (people, time, money) from other functions that may be more valuable.
So, if you think that a compile-time construct such as enumcount(Item.Type) is a valuable addition to the language, then you can more than post the Connect clause and see if it gets to the top of the list of functions.
But I need this number as a constant number, so I can use it in the Unity function [Range (int, int)].
One imperfect workaround is to define a constant that matches the current number of enumeration elements and throw an exception at runtime if the counters do not match:
Define a public constant next to your listing by commenting on it so that developers know to update it:
// Update this value any time the Type enum is updated public const int TypeCount = 5; public Enum Type { Bar1, Bar2, Bar3, Bar4, Bar5, }
use it in your attribute:
[Range(0, Item.TypeCount)] public void BlahBlahBlah() {}
and check it at the beginning of your application:
public static Main() { if(Enum.GetNames(typeof(Item.Type)).Length != Item.TypeCount) throw new ApplicationException ("TypeCount and number of Types do not match.\nPlease update TypeCount constant.") }