crayden
crayden

Reputation: 2280

How to use Numbers as C# enum Values

This is valid:

public enum Size
{
    Eight,
    EightAndOneHalf
};

But how can numbers be used in a C# enum like the following? Is it even possible? Or is the a preferred way to represent this structure?

public enum Size
{
    8,
    8.5
};

Upvotes: 0

Views: 3407

Answers (3)

Christopher
Christopher

Reputation: 9824

At their core Enums are little more then compile time constants, wich are grouped and thus get some type safety. It has the benefits of both a custom type, being only numbers for processing and using constants in code.

The default type of Enums is one of the integer ones and the default values start at 0, counting up. While the type and values can be overwritten, they can not be freely changed. It must be a integer one.

As enums effectively map to integers and are even implicitly converted (or rather left unconverted), you can do something like that with a enum/array combination.

Maybe a Dictionary<string, double> would be more fitting for your case?

Also there is a workaround: Just store 85 and 80 as the Enum values. Divide by 10 during output. Such tricks are often used to get around floating point inprecision. But it might work here too.

Personally I feel as if this falls into the area of "Internationalisation". "8.5" or "8,5" is simply how you display the value for that ShoeSize. And for Enums I always adivse to have a Array of values to output.

Upvotes: 0

Gauravsa
Gauravsa

Reputation: 6528

you can use structs:

public struct Size
{
        public const double Eight = 8;
        public const double EightPointFive = 8.5;
        public const double Nine = 9;
        // Add Remaining units / values
}

And then can be used like:

double size = Size.Eight;

Or can do like this:

enum Size {
    Eight, EightPointFive, Nine
}

and then use a function like this:

public float GetSize(Size size)
{
    switch (size)
    {
        case Size.Eight :
            return 8;
        case Size.EightPointFive :
            return 8.5;
        // rest of the sizes.
        default: return -1;
    }

    //unhandled
    return -1;
}

Upvotes: 4

Tieson T.
Tieson T.

Reputation: 21239

If what you're after is named constants, you can create something like this:

public static class Size
{
    public const double Eight = 8.0;
    public const double EightAndOneHalf = 8.5;
}

Then you would use it like:

var mySize = Size.EightAndOneHalf;

You can't enumerate your "fake" enum, though, so you'd have to add something like this, if you want to do that:

public static class Size
{
    public const double Eight = 8.0;
    public const double EightAndOneHalf = 8.5;

    public static IEnumerable<double> All = new double[] { Eight, EightAndOneHalf };
}

Then you can do something like:

foreach(var size in Size.All)
{
    // code here
}

This basic idea is used in the framework itself. For example: https://github.com/aspnet/Identity/blob/master/src/Identity/IdentityConstants.cs

Upvotes: 1

Related Questions