如何在 C# 中传递多个枚举值?
有时,在阅读其他人的 C# 代码时,我会看到一种方法在单个参数中接受多个枚举值。 我一直以为它很整洁,但从未仔细研究过。
好吧,现在我想我可能需要它,但不知道如何
- 设置方法签名来接受这项
- 工作,并使用方法中的值
- 定义枚举
来实现此类事情。
In my particular situation, I would like to use the System.DayOfWeek, which is defined as:
[Serializable]
[ComVisible(true)]
public enum DayOfWeek
{
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6
}
我希望能够将一个或多个 DayOfWeek 值传递给我的方法。 我可以按原样使用这个特定的枚举吗? 我该如何做上面列出的 3 件事?
Sometimes when reading others' C# code I see a method that will accept multiple enum values in a single parameter. I always thought it was kind of neat, but never looked into it.
Well, now I think I may have a need for it, but don't know how to
- set up the method signature to accept this
- work with the values in the method
- define the enum
to achieve this sort of thing.
In my particular situation, I would like to use the System.DayOfWeek, which is defined as:
[Serializable]
[ComVisible(true)]
public enum DayOfWeek
{
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6
}
I want to be able to pass one or more of the DayOfWeek values to my method. Will I be able to use this particular enum as it is? How do I do the 3 things listed above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我认为更优雅的解决方案是使用 HasFlag():
I think the more elegant solution is to use HasFlag():
我赞同里德的回答。 但是,在创建枚举时,您必须指定每个枚举成员的值,以便它形成一种位字段。 例如:
I second Reed's answer. However, when creating the enum, you must specify the values for each enum member so it makes a sort of bit field. For example:
您不能将 System.DayOfWeek 用作
[Flags]
枚举,因为您无法控制它。 如果您希望有一个接受多个DayOfWeek
的方法,那么您必须使用params
关键字,否则您可以创建自己的
[Flags]
许多其他响应者概述的枚举并使用按位比较。You can not use the System.DayOfWeek as a
[Flags]
enumeration because you have no control over it. If you wish to have a method that accepts multipleDayOfWeek
then you will have to use theparams
keywordOtherwise you can create your own
[Flags]
enumeration as outlined by numerous other responders and use bitwise comparisons.您必须指定数字,并像这样递增它们,因为它以按位方式存储值。
然后只需定义您的方法来获取此枚举
并调用它,执行类似的
操作来检查是否包含其中一个枚举值,使用按位运算来检查它们,例如
You have to specify the numbers, and increment them like this because it is storing the values in a bitwise fashion.
Then just define your method to take this enum
and to call it do something like
To check if one of the enum values was included check them using bitwise operations like
以此格式调用方法
实现 EnumToArray 方法以获取传递的选项
Call the method in this format
Implement a EnumToArray method to get the options passed
使用 [Flags] 属性标记您的枚举。 还要确保您的所有值都是互斥的(两个值不能加起来等于另一个值),例如您的情况下的 1,2,4,8,16,32,64
当您有一个接受 DayOfWeek 枚举的方法时按位或运算符 (|) 一起使用多个成员。 例如:
要检查参数是否包含特定成员,请对要检查的成员使用按位 and 运算符 (&)。
Mark your enum with the [Flags] attribute. Also ensure that all of your values are mutually exclusive (two values can't add up to equal another) like 1,2,4,8,16,32,64 in your case
When you have a method that accepts a DayOfWeek enum use the bitwise or operator (|) to use multiple members together. For example:
To check if the parameter contains a specific member, use the bitwise and operator (&) with the member you are checking for.
里德·科普西(Reed Copsey)是正确的,如果可以的话,我会添加到原始帖子中,但我不能,所以我必须回复。
在任何旧枚举上仅使用 [Flags] 是危险的。 我相信在使用标志时,您必须显式地将枚举值更改为 2 的幂,以避免值发生冲突。 请参阅FlagsAttribute 和 Enum 指南。
Reed Copsey is correct and I would add to the original post if I could, but I cant so I'll have to reply instead.
Its dangerous to just use [Flags] on any old enum. I believe you have to explicitly change the enum values to powers of two when using flags, to avoid clashes in the values. See the guidelines for FlagsAttribute and Enum.
借助发布的答案和这些:
我感觉我很明白。
谢谢。
With the help of the posted answers and these:
I feel like I understand it pretty well.
Thanks.
这种性质的东西应该表明您正在寻找什么:
Something of this nature should show what you are looking for:
当你定义枚举时,只需用 [Flags] 赋予它属性,将值设置为 2 的幂,它就会以这种方式工作。
除了将多个值传递给函数之外,没有其他任何变化。
例如:
有关更多详细信息,请参阅MSDN 有关枚举类型的文档。
编辑以回应问题的补充。
您将无法按原样使用该枚举,除非您想要执行诸如将其作为数组/集合/参数数组传递之类的操作。 这会让你传递多个值。 标志语法要求将枚举指定为标志(或以未设计的方式破坏语言)。
When you define the enum, just attribute it with [Flags], set values to powers of two, and it will work this way.
Nothing else changes, other than passing multiple values into a function.
For example:
For more details, see MSDN's documentation on Enumeration Types.
Edit in response to additions to question.
You won't be able to use that enum as is, unless you wanted to do something like pass it as an array/collection/params array. That would let you pass multiple values. The flags syntax requires the Enum to be specified as flags (or to bastardize the language in a way that's its not designed).