C# - How to Initialize Flags Enumerations



The most common way to initialize flags is to use hexadecimal literals.  This is how Microsoft and most C# developers do it:

[Flags]
public enum DaysOfTheWeek
{
	None = 0,
	Sunday = 0x01,
	Monday = 0x02,
	Tuesday = 0x04,
	Wednesday = 0x08,
	Thursday = 0x10,
	Friday = 0x20,
	Saturday = 0x40,
}

An even better method might be to use binary literals such as 0b0001, 0b0010, 0b0100, 0b1000, etc., but C# does not provide binary literals.  However, you can simulate a binary literal with the bit shift
operator.  This method has the advantage that the numbers visually increase by 1, so it’s very easy to spot errors and see which bit is set for each flag.  Note that this method does not affect program performance because the flag values are calculated at
compile time.

[Flags]
public enum DaysOfTheWeek
{
	None = 0,
	Sunday = 1 << 0,
	Monday = 1 << 1,
	Tuesday = 1 << 2,
	Wednesday = 1 << 3,
	Thursday = 1 << 4,
	Friday = 1 << 5,
	Saturday = 1 << 6,
}
时间: 2024-10-06 17:52:45

C# - How to Initialize Flags Enumerations的相关文章

Mesos原理与代码分析(4) Mesos Master的启动之三

3. ModuleManager::load(flags.modules.get())如果有参数--modules或者--modules_dir=dirpath,则会将路径中的so文件load进来 ? 代码中加载模块的代码如下 ? 对应的命令行参数如下: ? ? 都可以写什么Module呢? ? 首先是Allocator ? 默认是内置的Hierarchical Dominant Resource Fairness allocator ? 要写一个自己的Allocator: 通过--module

[swmm]调用存储数据的“结构指针数组”

目的: swmm读取.inp文件后,会进行一定计算(如计算conduit.slope)并将信息存储在各个指针变量中,如:TConduit* Conduit等.通过在python中调用该指针,能更快捷的编程. 思路步骤: 根据swmm5.c,编写Swmm5Extend.c: 删去读取文件后的模拟函数: 重写便于python使用的swmm5_extend_run(): 编写将object数量存储在全局变量中的函数count_object(): 将函数与全局变量都放在Swmm5Extend.h中: 编

C# 2012 step by step 学习笔记8 CHAPTER 9 Creating Value types with enumerations and Structures

C# 2012 step by step 学习笔记8 CHAPTER 9 Creating Value types with enumerations and Structures things about 1. Declare an enumeration type. 2. Create and use an enumeration type. 3. Declare a structure type. 4. Create and use a structure type. 5. Explain

IPVS: Can&#39;t initialize ipvs: Protocol not available解决方法

经排查发现本地的测试环境磁盘空间满了,造成数据库不能写入数据. 最终的罪魁祸首是日志文件. 由于我们使用的是lvs负载均衡,前端的Keepalived启动之后不能初始化ipvs协议,所以日志在不停的刷一下的内容,速度之快令人乍舌,日志文件的大小在不停的疯长,最终导致空间不足,杯具的事情就这样发生了. 错误内容 查看系统日志文件内容 tail -f /var/log/messages 你会看见很多如下的信息在不停的跳动. May 16 08:35:22 linux-node1 Keepalived

懒惰的 initialize 方法

因为 ObjC 的 runtime 只能在 Mac OS 下才能编译,所以文章中的代码都是在 Mac OS,也就是 x86_64 架构下运行的,对于在 arm64 中运行的代码会特别说明. 写在前面 这篇文章可能是对 Objective-C 源代码解析系列文章中最短的一篇了,在 Objective-C 中,我们总是会同时想到 load.initialize 这两个类方法.而这两个方法也经常在一起比较: 在上一篇介绍 load 方法的文章中,已经对 load 方法的调用时机.调用顺序进行了详细地分

在WCF中使用Flag Enumerations

请看MSDN示例: [DataContract][Flags]public enum CarFeatures{    None = 0,    [EnumMember]    AirConditioner = 1,    [EnumMember]    AutomaticTransmission = 2,    [EnumMember]    PowerDoors = 4,    AlloyWheels = 8,    DeluxePackage = AirConditioner | Autom

iOS中 性能优化之浅谈load与initialize

一. +load 源码分析 extern bool hasLoadMethods(const headerType *mhdr); extern void prepare_load_methods(const headerType *mhdr); void load_images(const char *path __unused, const struct mach_header *mh) { // Return without taking locks if there are no +lo

iOS - Swift Enumerations or how to annoy Tom

@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(htt

Initialize the shader

目录 Loads the shader files and makes it usable to DirectX and the GPU Compile the shader programs into buffers D3DCompileFromFile function Create the shader objects ID3D11Device::CreateVertexShader method Create the layout of the vertex data D3D11_INP