使用[Flags]和按位操作符优化C#枚举处理
创始人
2024-12-28 05:37:12
0

在C#开发中,我们经常会遇到需要检查多个枚举值的情况。为了使代码更加简洁和可读,我们可以利用[Flags]特性和按位操作符来简化枚举值的比较

public enum EnumShapeType {     None = 0,     PointType,     LineType,     CircleType,     BoxType,     RectangleType,     SphereType,     CutFeatureType,     CommonFeatureType,     SweepFeatureType,     FuseFeatureType,     FilletFeatureType,     HoleFeatureType,     ExtrudeFeatureType,     RevolveFeatureType,     PolygonFaceFeatureType,     PointsLineFeatureType,     PlanarFaceFeatureType } if(enumShapeType == EnumShapeType.BoxType || enumShapeType == EnumShapeType.SphereType || enumShapeType == EnumShapeType.LineType || enumShapeType == EnumShapeType.PointType || enumShapeType == EnumShapeType.RectangleType) {     // Your code here } 

这段代码检查enumShapeType是否为EnumShapeType枚举中的五种特定类型之一:BoxType、SphereType、LineType、RectangleType和PointType

具体来说,代码使用多个逻辑或 (||) 运算符来逐一比较enumShapeType与每个目标枚举值。这种方法虽然直观,但当枚举值较多时,代码会变得冗长且难以维护

可以使用Enum类的HasFlag方法来简化这段代码

确保你的枚举类型具有[Flags]特性,每个枚举成员的值是2的幂(1, 2, 4, 8, 等),以确保它们的二进制表示形式是唯一的,这是使用[Flags]特性的关键。这样可以正确地使用按位操作符

[Flags] public enum EnumShapeType {     None = 0,     PointType= 1 << 0,     LineType = 1 << 1,     CircleType = 1 << 2,     BoxType = 1 << 3,     RectangleType = 1 << 4,     SphereType = 1 << 5,     CutFeatureType = 1 << 6,     CommonFeatureType =  1 << 7,     SweepFeatureType = 1 << 8,     FuseFeatureType = 1 << 9,     FilletFeatureType = 1 << 10,     HoleFeatureType = 1 << 11,     ExtrudeFeatureType = 1 << 12,     RevolveFeatureType = 1 << 13,     PolygonFaceFeatureType = 1 << 14,     PointsLineFeatureType = 1 << 15,     PlanarFaceFeatureType = 1 << 16 } 

1 << n表示将1左移n位,相当于2的n次方

以下是每个枚举值及其对应的数值:

枚举值数值二级制十六进制数值
None00000 0000 0000 00000x0
PointType10000 0000 0000 00010x1
LineType20000 0000 0000 00100x2
CircleType40000 0000 0000 01000x4
BoxType80000 0000 0000 10000x8
RectangleType160000 0000 0001 00000x10
SphereType320000 0000 0010 00000x20
CutFeatureType640000 0000 0100 00000x40
CommonFeatureType1280000 0000 1000 00000x80
SweepFeatureType2560000 0001 0000 00000x100
FuseFeatureType5120000 0010 0000 00000x200
FilletFeatureType10240000 0100 0000 00000x400
HoleFeatureType20480000 1000 0000 00000x800
ExtrudeFeatureType40960001 0000 0000 00000x1000
RevolveFeatureType81920010 0000 0000 00000x2000
PolygonFaceFeatureType163840100 0000 0000 00000x4000
PointsLineFeatureType327681000 0000 0000 00000x8000
PlanarFaceFeatureType655361 0000 0000 0000 00000x10000

注意

  • C#中枚举(enum)的默认基础类型是int。这意味着在没有明确指定基础类型的情况下,枚举的每个成员将被存储为一个32位的有符号整数 (int)

  • 对于int类型,使用位标志枚举时的最大值是2147483648(1 << 31)

  • 如果需要更多的标志位,可以将枚举的基础类型更改为long,这样可以定义最多64个标志位

[Flags] public enum EnumShapeType : long {     None = 0,     PointType = 1L << 0,     LineType = 1L << 1,     CircleType = 1L << 2,     // ... 其他枚举值 ...     PlanarFaceFeatureType = 1L << 16,     AnotherFeatureType = 1L << 32, // 4294967296     MaxValue = 1L << 63            // 9223372036854775808 } 

使用按位操作简化检查

在简化的代码中,可以使用按位或操作符|来创建一个掩码,并使用按位与操作符&来检查多个枚举成员

EnumShapeType shapeTypesToCheck = EnumShapeType.BoxType | EnumShapeType.SphereType | EnumShapeType.LineType | EnumShapeType.PointType | EnumShapeType.RectangleType;  if ((enumShapeType & shapeTypesToCheck) != EnumShapeType.None) {     // Your code here } 

使用数组和Contains方法简化

另外一种更直观和易于理解的方式(不使用[Flags]特性),可以使用一个数组来存储需要检查的枚举成员,然后利用Contains方法来简化代码

EnumShapeType[] validShapeTypes = { EnumShapeType.BoxType, EnumShapeType.SphereType, EnumShapeType.LineType, EnumShapeType.PointType, EnumShapeType.RectangleType };  if (validShapeTypes.Contains(enumShapeType)) {     // Your code here } 

相关内容

热门资讯

专业讨论!德扑之星真破解套路(... 专业讨论!德扑之星真破解套路(辅助挂)软件透明挂(有挂了解)-哔哩哔哩;人气非常高,ai更新快且高清...
每日必看!智星德州菠萝外挂检测... 每日必看!智星德州菠萝外挂检测(辅助挂)软件透明挂(有挂教学)-哔哩哔哩1、玩家可以在智星德州菠萝外...
透视透明挂!轰趴十三水有后台(... 轰趴十三水有后台赢率提升策略‌;透视透明挂!轰趴十三水有后台(辅助挂)软件透明挂(有挂详情)-哔哩哔...
发现玩家!德扑ai助手软件(辅... 发现玩家!德扑ai助手软件(辅助挂)透视辅助(有挂教学)-哔哩哔哩;玩家在德扑ai助手软件中需先进行...
一分钟了解!x-poker辅助... 一分钟了解!x-poker辅助软件(辅助挂)辅助透视(有挂攻略)-哔哩哔哩1、每一步都需要思考,不同...
一分钟揭秘!德州最新辅助器(辅... 一分钟揭秘!德州最新辅助器(辅助挂)透视辅助(有挂攻略)-哔哩哔哩;德州最新辅助器最新版本免费下载安...
玩家攻略推荐!德州辅助(辅助挂... 玩家攻略推荐!德州辅助(辅助挂)辅助透视(有挂了解)-哔哩哔哩是由北京得德州辅助黑科技有限公司精心研...
揭秘真相!pokernow德州... 《揭秘真相!pokernow德州(辅助挂)辅助透视(有挂介绍)-哔哩哔哩》 pokernow德州软件...
五分钟了解!德州之星辅助器(辅... 五分钟了解!德州之星辅助器(辅助挂)辅助透视(有挂透明)-哔哩哔哩1、很好的工具软件,可以解锁游戏的...
推荐一款!pokermaste... 1、推荐一款!pokermaster有外挂(辅助挂)透视辅助(有挂教学)-哔哩哔哩;详细教程。2、p...