VB.NET Enumeration Representation
Is 开发者_如何学JAVAit guaranteed that the numeric values for an Enum
with only uninitialized values start at zero and increment by one in the order defined?
Yes. From the documentation:
If you do not specify initializer for a member, Visual Basic initializes it either to zero (if it is the first member in member list), or to a value greater by one than that of the immediately preceding member.
http://msdn.microsoft.com/en-us/library/8h84wky1(VS.80).aspx
Yes. If you set one explicitly, the ones after it also increment.
That allows you to have:
class Program
{
static void Main(string[] args)
{
Console.WriteLine((int)Seasons.Spring);
Console.WriteLine((int)Seasons.Summer);
Console.WriteLine((int)Seasons.Autumn);
Console.WriteLine((int)Seasons.Fall);
Console.WriteLine((int)Seasons.Winter);
Console.Read();
}
}
public enum Seasons
{
Spring,
Summer,
Autumn,
Fall = Autumn,
Winter
}
Output: 0 1 2 2 3
And they will have values 0, 1, 2, 3 and Fall will have the same value as Autumn.
Sorry, my example is in C# but the same applies for VB.net
精彩评论