枚举VS静态类(普通和带字符串值) [英] Enum VS Static Class (Normal and With String Values)

查看:71
本文介绍了枚举VS静态类(普通和带字符串值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在为Windows Mobile和Android进行开发.我对这两个概念感到困惑.

I have been developing for windows mobile and android for sometime. And I'm confused about these two concepts.

假设我要根据用户设备的某些屏幕尺寸做出决定.因此,我期待如此的预定义值.我可以使用switch语句来处理我的逻辑.但是我不确定是否应该为此目的使用静态类的枚举.哪一种是更好的方法.我可以用这两种不同的方式来做我的逻辑.哪一种是正确的方法?我糊涂了.我是否还可以使用String值??由于当前我坚持使用类,因此我需要更新以使用所有枚举.那么,如何将我的Class更改为String Enum?反正.谢谢.

Let's say I want to make decision based on some the user's device Screen Size. So I'll expect so predefined values. I could use a switch statement for handling my logic. But I'm not sure whether I should use Enum of a Static Class for this purpose. Which one is a better approach. I can do my logic in these two different ways. Which one is the correct approach? I'm confused. And is it possible for me to use String values also? Because currently I'm sticking with classes, I need to update to use all enums. So How about changing my Class to String Enum? Any way. Thanks.

使用枚举

//My predefined values
public enum ScreenSizeEnum
{
    Small, Medium, Large, XLarge,
}
//Handling Logic
private void SetScreenSize(ScreenSizeEnum Screen)
{
    switch (Screen)
    {
        case ScreenSizeEnum.Large:
            //Do Logic
            break;
        case ScreenSizeEnum.Small:
            //Do Logic
            break;
    }
}

使用课程

//My predefined values
public class ScreenSizeClass
{
    public const int Small = 0;
    public const int Medium = 1;
    public const int Large = 2;
    public const int XLarge = 3;
}
//Handling Logic
private void SetScreenSize(int Screen)
{
    switch (Screen)
    {
        case ScreenSizeClass.Large:
            //Do Logic
            break;
        case ScreenSizeClass.Small:
            //Do Logic
            break;
    }
}

推荐答案

来自

枚举类型(也称为枚举或枚举)提供了定义一组命名整数常量的有效方法,该常量可以是分配给变量.

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. 您清楚地为客户端代码指定了对变量有效的值.

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

在Visual Studio中,IntelliSense列出了定义的值.

In Visual Studio, IntelliSense lists the defined values.

因此,如果您传递 enum ,则它是强类型的,因此您可以自动控制可以传递给方法的内容.

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

ScreenSizeEnum size = ScreenSizeEnum.Medium;
SetScreenSize(size); 

使用 const static 字段时,您明确需要检查传递的 int 值是否来自预期的分隔符.

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

int somevalue = ...;//anything
SetScreenSize(somevalue); //compiles

private void SetScreenSize(int Screen)
{
    switch (Screen)
    {
        case ScreenSizeClass.Large:
            //Do Logic
            break;
        case ScreenSizeClass.Small:
            //Do Logic
            break;
        default: 
            // something else, what to do??
            break;
    }
}


基于评论:

如果需要检查,是否在 enum 中定义了某些 int ,则可以执行以下操作:

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
}

或扩展方法:

public static T GetEnumValue<T>(this string value) where T : struct
{
    Type t = typeof(T);
    if (!t.IsEnum)
        throw new Exception("T must be an enum");
    else
    {
        T result;
        if (Enum.TryParse<T>(value, out result))
            return result;
        else
            return default(T);
    }
}

可以使用

int somevalue = 1;
ScreenSizeEnum size = somevalue.GetEnumValue<ScreenSizeEnum>();


关于 string (基于OP的已修改问题):


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

来自枚举(C#参考):

枚举的批准类型为byte,sbyte,short,ushort,int,uint,long或ulong.

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

所以您不能有一个字符串枚举.但是您可以使用枚举中的名称,因为 ToString 方法返回名称,而不是枚举的值.

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:

public static T GetEnumValue<T>(this string value) where T : struct
{
    Type t = typeof(T);
    if (!t.IsEnum)
        throw new Exception("T must be an enum");
    else
    {
        if (Enum.IsDefined(t, value))
            return (T)Enum.Parse(t, value);
        else
            return default(T);
    }
}

如此

int i = (int)ScreenSizeEnum.Small;
string str = ScreenSizeEnum.Small.ToString();
ScreenSizeEnum isize = i.GetEnumValue<ScreenSizeEnum>();//ScreenSizeEnum.Small
ScreenSizeEnum strsize = str.GetEnumValue<ScreenSizeEnum>();//ScreenSizeEnum.Small

这篇关于枚举VS静态类(普通和带字符串值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆