设计一个带有 System.Enum 类型参数的方法是否可以接受?

发布于 2024-08-05 00:05:47 字数 1431 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

燕归巢 2024-08-12 00:05:47

使用枚举值作为参数是绝对有效的。

以这种方式使用枚举是使用字符串常量(...)的可接受的替代方案吗?

是的,事实上这是枚举的主要用途之一。

使用通用枚举值不太明显,因为您通常想知道可以期望什么值。枚举值的常见用途是在 switch 语句中使用它,它有助于了解预期的值。枚举值的名称通常并不那么重要,重要的是它所代表的内容。

It's absolutely valid to use an enum value as a parameter.

Is using enum in this way an acceptable alternative to using string constants(...)?

Yes, in fact this is one of the main uses of enums.

Using a general Enum value is a little less obvious, since you generally want to know what values you can expect. A common use of enum values is to use it in a switch statement, and it helps to know what values can be expected. The name of an enum value is not generally that important, it's what it represents.

酷到爆炸 2024-08-12 00:05:47

是的,使用 Enum 作为参数类型是完全有效的。

Yes, using Enum as a parameter type is completely valid.

迷途知返 2024-08-12 00:05:47

最近使用 Enum 类的示例是扩展它以获取指定 Enum 值的 DescriptionAttribute: 链接

A recent example of using the Enum class was to extend it to get the DescriptionAttribute of a specified Enum value: link.

甜中书 2024-08-12 00:05:47

是的,这是完全有效的。但请确保枚举的可见性等于方法的可见性。

换句话说:如果您的方法是公共的,则将枚举设为公共。否则您将无法调用该方法。

String 的 Equals 方法使用枚举作为比较选项的可选第二个参数:

String s = "Hello";
s.Equals("hello", StringComparison.OrdinalIgnoreCase);

编辑:
重读你的问题后我明白你在问什么。我个人不认为传递 Enum 元素仅将其转换为字符串有什么好处。

我猜您正在尝试容纳几种不同的派生枚举类型?将枚举存储到字典中或将参数限制为特定的派生实例会更好吗?

如果您允许将任何派生枚举传递到该方法中,那么您就无法解决键唯一性的问题。

Yes it is completely valid. But make sure that the enum's visibility is equal to the method's visibility.

In other words: If your method is public, make the enum public. Otherwise you'd be unable to invoke the method.

The String's Equals method uses an enum as the optional second parameter for comparison options:

String s = "Hello";
s.Equals("hello", StringComparison.OrdinalIgnoreCase);

EDIT:
After rereading your question I understand what you are asking. I personally don't see the benefit of passing an Enum element to only convert it to a string.

I guess you are trying to accommodate several different derived enum types? Would it be better to either store the enum into the dictionary, or limit the parameter to a specific derived instance?

You aren't solving the problem of key uniqueness if you are allowing any derived enum to be passed into the method.

本宫微胖 2024-08-12 00:05:47

如果您设计一个工厂模式来为具有不同用途的不同产品生成类,当然还有一个共同的基础。但所有这些产品都有描述其行为或状态的枚举。

您可以有一种方法在基类中生成这些产品
喜欢

public class ProductsFactory
{
public static BaseProduct Create( string ProductName, Enum State)
{
....
  return new ProductX;
}
}
public class SailBoat: BaseProduct
{
public enum BoatState
{
Sailing,
Docked,
}
}

public class Airplain: BaseProduct
{
public enum AirplainState
{
Flying,
Landing,
TakingOff
}
}

void main
{
ProductFactory.Create("SailBoat", SailBoat.BoatState.Sailing);
ProductFactory.Create("Airplain", Airplain.AirplainState.Flying);
}

If you design a factory pattern to generate classes for different Products with different uses and of course a common base. But all of this products has Enums that describes their actions or States.

You could have a method to generate those products in the base class
like

public class ProductsFactory
{
public static BaseProduct Create( string ProductName, Enum State)
{
....
  return new ProductX;
}
}
public class SailBoat: BaseProduct
{
public enum BoatState
{
Sailing,
Docked,
}
}

public class Airplain: BaseProduct
{
public enum AirplainState
{
Flying,
Landing,
TakingOff
}
}

void main
{
ProductFactory.Create("SailBoat", SailBoat.BoatState.Sailing);
ProductFactory.Create("Airplain", Airplain.AirplainState.Flying);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文