gpt4 book ai didi

c# - 测试按位枚举值

转载 作者:可可西里 更新时间:2023-11-01 02:59:20 26 4
gpt4 key购买 nike

我以前没有真正使用过按位枚举,我只是想确保我的测试是正确的。我最感兴趣的是测试值 None 和 All。我们从使用此枚举对某些数据片段进行分类的 Web 服务接收数据。鉴于此,我假设 None 和 All 都不会与任何其他值组合。

给定以下按位枚举定义;

[System.FlagsAttribute()]
public enum TrainingComponentTypes : int
{
None = 0,
AccreditedCourse = 1,
Qualification = 2,
Unit = 4,
SkillSet = 8,
UnitContextualisation = 16,
TrainingPackage = 32,
AccreditedCourseModule = 64,
All = 127,
}

我在 this MSDN site 上阅读了以下引述关于标志属性;

Use None as the name of the flag enumerated constant whose value is zero. You cannot use the None enumerated constant in a bitwise AND operation to test for a flag because the result is always zero. However, you can perform a logical, not a bitwise, comparison between the numeric value and the None enumerated constant to determine whether any bits in the numeric value are set.

此实例中的逻辑比较是否指的是枚举的正常相等性测试?例如;

TrainingComponentTypes tct = TrainingComponentTypes.None; 
if (tct == TrainingComponentTypes.None)
{ ... }

对于按位比较,我正在执行以下操作;

 TrainingComponentTypes tct = TrainingComponentTypes.AccreditedCourse | TrainingComponentTypes.Qualification | TrainingComponentTypes.TrainingPackage;
Assert.IsTrue((tct & TrainingComponentTypes.AccreditedCourse) == TrainingComponentTypes.AccreditedCourse, "Expected AccreditedCourse as part the enum");

Assert.IsFalse((tct & TrainingComponentTypes.SkillSet) == TrainingComponentTypes.SkillSet, "Found unexpected SkillSet as part the enum");

最后,在进行所有测试时,我尝试了逻辑比较和按位比较,它们都返回相同的结果。我应该在这里使用一个吗?例如;

TrainingComponentTypes tct = TrainingComponentTypes.All;

Assert.IsTrue((tct & TrainingComponentTypes.All) == TrainingComponentTypes.All, "Expected All as part the enum");
Assert.IsTrue((tct) == TrainingComponentTypes.All, "Expected All as part the enum");
// The follow also pass the assertion for a value of All
Assert.IsTrue((tct & TrainingComponentTypes.Qualification) == TrainingComponentTypes.Qualification, "Expected Qualification as part the enum");
Assert.IsTrue((tct & TrainingComponentTypes.TrainingPackage) == TrainingComponentTypes.TrainingPackage, "Expected TrainingPackage as part the enum");

总而言之,我想了解以下有关按位枚举的信息;

  1. 我的理解是否合乎逻辑以我的例子比较正确多于?
  2. 是我的表现方式按位比较正确吗?
  3. 处理“全部”的正确方法是什么值(按位或逻辑)。我不确定我们是否会收到一个 All 与其他 TrainingComponentTypes 结合的值。我不明白为什么我们会,但是,你永远不知道?
  4. 我假设那个开关是对的吗陈述基本上不应该用于按位枚举(给定 none似乎是一个特例,并且需要逻辑比较)?

谢谢,克里斯

最佳答案

简短回答:是 :)

更长:

1)所有的操作都是对flags变量的整型值进行的,所以可以这样想。

2) 是的。

3) 两者都有效。但是,值得注意的是,如果有人将无效值推送到变量中,则 == TrainingComponentTypes.All 版本将失败。例如:

var badValue = (TrainingComponentTypes)128 | TrainingComponentTypes.All;
// now badValue != TrainingComponentTypes.All
// but (badValue & TrainingComponentTypes.All) == TrainingComponentTypes.All

对于这部分:

I am not sure if we'd ever receive a value where All was combined with other TrainingComponentTypes.

我不确定您是否完全理解枚举在幕后是如何工作的。

The value of All is:
127 = 1111111 (binary)

The other values are:
AccreditedCourse = 0000001
Qualification = 0000010
Unit = 0000100
SkillSet = 0001000
UnitContextualisation = 0010000
TrainingPackage = 0100000
AccreditedCourseModule = 1000000

如您所见,All 只是所有这些值的按位 | 组合在一起。您不能将任何其他 TraningComponentType 与 All 组合,因为 All 已经包含它们!此外,如果您自己将它们与 | 组合在一起,则与直接使用 All 完全相同(因此,当您在枚举中定义它时,All 只是一种便利)。

4) 您可以使用它来检查 None 或 All,但不能检查其他值。

值得注意的是,Enum 上有一个方便的方法可以为您进行这些检查:Enum.HasFlag .

关于c# - 测试按位枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6105655/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com