Are ranges with enumerations possible?

In C #, you can use ranges of numbers in enum types, e.g.

public enum BookType
{
    Novel = 1,
    Journal = 2,
    Reference = 3,
    TextBook = 4 .. 10
}

EDIT: The reason this is necessary is to distinguish from a number to an enumeration type, for example:

int iBook = 5
BookType btBook = (BookType)ibook
Debug.Print "Book " + ibook + " is a " btBook

and expected result: Book 5 is a TextBook

+5
source share
9 answers

In accordance with the C # standard (p612, C # programming language), the value specified for the listing must be a constant integer (or any similar type - long, byte, sbyte, short, etc.), so the range of values ​​isn ' t valid.

My compiler (VS2008) agrees with the specification.

Since you cannot repeat the names in an enumeration, the closest you will get something like this:

public enum BookType
{
    Novel = 1,
    Journal = 2,
    Reference = 3,
    TextBook4 = 4,
    TextBook5 = 5, ...
    TextBook10 = 10
}

. , ...

+8

, , . enum, :

[Flags]
public enum BookType
{
    Novel = 0,
    Journal = 1 << 0,
    Reference = 1 << 1,
    TextBook1 = 1 << 2,
    TextBook2 = 1 << 3,
    TextBook3 = 1 << 4,
    TextBook4 = 1 << 5,
    TextBook5 = 1 << 6,
    TextBooks1To5 = TextBook1 | TextBook2 | TextBook3 | TextBook4 | TextBook5
}
+7

, , readonly.

. - :

public sealed class BookType
{
    public static readonly BookType Novel = new BookType(1, 1, "Novel");
    public static readonly BookType Journal = new BookType(2, 2, "Journal");
    public static readonly BookType Reference = new BookType(3, 3, "Reference");
    public static readonly BookType Textbook = new BookType(4, 10, "Textbook");

    public int Low { get; private set; }
    public int High { get; private set; }
    private string name;

    private static class BookTypeLookup
    {
        public static readonly Dictionary<int, BookType> lookup = new Dictionary<int, BookType>();
    }

    private BookType(int low, int high, string name)
    {

        this.Low = low;
        this.High = high;
        this.name = name;

        for (int i = low; i <= high; i++)
            BookTypeLookup.lookup.Add(i, this);
    }

    public override string ToString()
    {
        return name;
    }

    public static implicit operator BookType(int value)
    {
        BookType result = null;
        if (BookTypeLookup.lookup.TryGetValue(value, out result))
            return result;

        throw new ArgumentOutOfRangeException("BookType not found");
    }
}

, , .

.

 var bookType = (BookType)5;
 Console.WriteLine(bookType);
+3

, , ( ):

    private enum eTiming
    {
        eOnTime = 0,
        eSlightDelay = 5,
        eMinorDelay = 15,
        eDelayed = 30,
        eMajorDelay = 45,
        eSevereDelay = 60
    }

    private static eTiming CheckIfTimeDelayed(TimeSpan tsTime)
    {
        eTiming etiming = eTiming.eOnTime;

        foreach (eTiming eT in Enum.GetValues(typeof(eTiming)))
        {
            if (Convert.ToInt16(eT) <= tsTime.TotalMinutes)
                etiming = eT;
        }

        return etiming;
    }

, (-).

+3

, . , , , . TextBook4, TextBook5 ..

+1

.

var BookType = new Dictionary<int, string>();
BookType.Add(1, "Novel");
BookType.Add(2, "Journal");
BookType.Add(3, "Reference");
BookType.Add(4, "TextBook");
BookType.Add(5, "TextBook");
BookType.Add(6, "TextBook");
BookType.Add(7, "TextBook");
BookType.Add(8, "TextBook");
BookType.Add(9, "TextBook");
BookType.Add(10, "TextBook");

int iBook = 5 
Debug.Print "Book " + iBook + " is a " BookType[iBook]

. , .

.

+1

, int . BookType (NovelTypes, JournalTypes ..).

  • BookType .
  • (16 .
  • , Novel = 3 .

:

class Program
{
    /// <summary> Number of subtypes reserved for each BookType. </summary>
    private const byte BookTypeStep = 16;
    /// <summary> Bitmask to use to extract BookType from a byte. </summary>
    private const byte BookTypeExtractor = Byte.MaxValue - BookTypeStep + 1;
    /// <summary> Bitmask to use to extract Book subtype from a byte. </summary>
    private const byte BookSubTypeExtractor = BookTypeStep -1;

    public enum BookType : byte
    {
        Unknown = 0,
        Novel = BookTypeStep * 1,
        Journal = BookTypeStep * 2,
        Reference = BookTypeStep * 3,
        TextBook = BookTypeStep * 4,
    }

    static void Main(string[] args)
    {
        for(int i = 16; i < 80; i++)
        {
            Console.WriteLine("{0}\tof type {1} ({2}),\tsubtype nr {3}",
                i,
                i & BookTypeExtractor,
                (BookType)(i & BookTypeExtractor),
                i & BookSubTypeExtractor
            );
        }
        Console.ReadLine();
    }
}

16-31 , 32-47 ..

+1

, , : ( , , , ) ?

# - :

public enum BookType
{
    Novel = 1,
    Journal = 2,
    Magazine = 2,
    Reference = 3,
    TextBook = 4,
    LabWorkbook = 4,
    Dictionary = 5,
    Encyclopedia = 5
}

, BookType.Journal, BookType.Magazine , 2. , - ( " # , ", ).

0

: . , :

public enum BookType
{
    Novel = 1,
    Journal = 2,
    Reference = 3,
    TextBook = 4
}

public void bookOutput(int book)
{
   if(book < 4)
       Console.Writeline("Book "+book+" is a " + ((BookType)book).ToString());
   else
       Console.Writeline("Book "+book+" is a " + BookType.TextBook.ToString());
}

, enum .

0

All Articles