枚举VS静态类(普通和带字符串值) [英] Enum VS Static Class (Normal and With String Values)
问题描述
一段时间以来,我一直在为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;
}
}