Skip to content Skip to sidebar Skip to footer

Enum Vs Static Class (normal And With String Values)

I have been developing for windows mobile and android for sometime. And I'm confused about these two concepts. Let's say I want to make decision based on some the user's device Scr

Solution 1:

From Enumeration Types (C# Programming Guide):

An enumeration type (also named an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable.

The following are advantages of using an enum instead of a numeric type:

  1. You clearly specify for client code which values are valid for the variable.

  2. In Visual Studio, IntelliSense lists the defined values.

So if you pass enum, it is strongly typed, so you automatically get control over what you can pass into a method.

ScreenSizeEnumsize= ScreenSizeEnum.Medium;
SetScreenSize(size); 

When using const or static fields you definetely need to check whether the passed int value is taken from the expected diapason.

int somevalue = ...;//anythingSetScreenSize(somevalue); //compilesprivatevoidSetScreenSize(int Screen){
    switch (Screen)
    {
        case ScreenSizeClass.Large:
            //Do Logicbreak;
        case ScreenSizeClass.Small:
            //Do Logicbreak;
        default: 
            // something else, what to do??break;
    }
}

Based on comments:

If it's necessary to check, whether some int is defined in enum, one can do something like this:

int somevallue = 0;
if(Enum.IsDefined(typeof(ScreenSizeEnum), somevallue))
{
    //it's ok
}

Or an extension method:

publicstatic T GetEnumValue<T>(thisstringvalue) where T : struct
{
    Type t = typeof(T);
    if (!t.IsEnum)
        thrownew Exception("T must be an enum");
    else
    {
        T result;
        if (Enum.TryParse<T>(value, out result))
            return result;
        elsereturndefault(T);
    }
}

which could be used

intsomevalue=1;
ScreenSizeEnumsize= somevalue.GetEnumValue<ScreenSizeEnum>();

As for the string (based on OP's edited question):

From enum (C# Reference):

The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.

So you cannot have an enum of strings. But you can use names from enums, as ToString method returns the name, not the value of the enum.

ScreenSizeEnum.Small.ToString(); //Small

So you can have another extension method on strings:

publicstatic T GetEnumValue<T>(thisstringvalue) where T : struct
{
    Type t = typeof(T);
    if (!t.IsEnum)
        thrownew Exception("T must be an enum");
    else
    {
        if (Enum.IsDefined(t, value))
            return (T)Enum.Parse(t, value);
        elsereturndefault(T);
    }
}

So that

inti= (int)ScreenSizeEnum.Small;
stringstr= ScreenSizeEnum.Small.ToString();
ScreenSizeEnumisize= i.GetEnumValue<ScreenSizeEnum>();//ScreenSizeEnum.SmallScreenSizeEnumstrsize= str.GetEnumValue<ScreenSizeEnum>();//ScreenSizeEnum.Small

Solution 2:

This is precisely what enums are there for. Not that you can't use the static class with the constants, but enum is by far cleaner...

Solution 3:

enums are basically used when you want a variable or parameter to have value from a fixed set of possible constants. You can replace enums with class with a set of static final int constants. But using enums is more flexible & readable appraoch over the later one.

Solution 4:

A class can be very handy if you want more as the enumerator only. If you want to implement several Get... functions that can return other things that you need.

My example is that I have a class of some type but there is a related other type that I need to.

Post a Comment for "Enum Vs Static Class (normal And With String Values)"