0

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
};
crayden
  • 2,130
  • 6
  • 36
  • 65
  • 8.5 is not an integer. Would a little abstraction help? For Size, you could have Large and Small – Ňɏssa Pøngjǣrdenlarp Nov 25 '18 at 21:50
  • You're right! Although, the 8 does not work either. And no, integers would be required for this use case, instead of "Small, Medium, Large...". – crayden Nov 25 '18 at 21:52
  • @crayden what does the number `8` alone signify? that's not how enums work. – Ousmane D. Nov 25 '18 at 21:53
  • @crayden _"The enum keyword is used to declare an enumeration, a distinct type that consists of **a set of named constants called the enumerator list**."_ as per the C# documentation. – Ousmane D. Nov 25 '18 at 21:54
  • I'm trying to define a finite set of shoe sizes. Not sure if enum is the most appropriate type to use, or another type instead. – crayden Nov 25 '18 at 21:56
  • int x = 8.5; is not valid. Whilst you can easily cast between an enum and its int value, no int can ever == 8.5 so your question is a bit moot. You can I guess maintain a Dictionary and Dictionary for your mappings. – Adam G Nov 25 '18 at 21:59

3 Answers3

4

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;
}
Gauravsa
  • 6,330
  • 2
  • 21
  • 30
  • Is it possible to bind this to a select list in the View? – crayden Nov 25 '18 at 23:01
  • @crayden There is a helper for that, if you want to go the enum route: https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.html.selectextensions.enumdropdownlistfor?view=aspnet-mvc-5.2 - requires you to be using version 5.1 or newer of the MVC framework. Example: https://stackoverflow.com/questions/388483/how-do-you-create-a-dropdownlist-from-an-enum-in-asp-net-mvc/22295360#22295360 – Tieson T. Nov 25 '18 at 23:26
1

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

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
0

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.

Christopher
  • 9,634
  • 2
  • 17
  • 31