Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum with int value?

Tags:

c#

enums

I want to make an enum for possible grades. This is a working example:

public enum Grade
{
    A, B, C, D, E, F
}

However, I want the grades to be integers, like

public enum Grade
{
    1, 2, 3, 4, 5
}

Why does the first one work but not the second? How can I make a similar variable that can only take values from 1-5 (and is nullable)?

like image 803
lte__ Avatar asked Apr 04 '17 13:04

lte__


1 Answers

You should specify Grade like this.

public enum Grade
{
    A = 1,
    B,
    C,
    D,
    E,
    F
}

B, C and so on will take next value.

like image 118
labilbe Avatar answered Nov 15 '22 05:11

labilbe