Home >Backend Development >C++ >C# Bitwise Enum Operations: A Concise Cheat Sheet?

C# Bitwise Enum Operations: A Concise Cheat Sheet?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-13 12:30:52480browse

C# Bitwise Enum Operations: A Concise Cheat Sheet?

Common bit operations in C# enumerations

This article aims to help developers understand common enumeration bit operations in C#.

Question:

When doing bitwise operations in enumerations, it's always confusing how to set, clear, toggle, or test individual bits. These operations are not commonly used, so they are easy to forget. A "bit operations cheat sheet" will be very helpful.

Example:

<code class="language-csharp">flags = flags | FlagsEnum.Bit4;  // 设置第 4 位。</code>

or

<code class="language-csharp">if ((flags & FlagsEnum.Bit4) == FlagsEnum.Bit4) // 是否有更简洁的方法?</code>

Can you provide examples for all other common operations? It's better to use C# syntax and [Flags] enumerations.

Answer:

To solve this problem, some developers created extension methods that extend System.Enum and these methods are more convenient to use.

<code class="language-csharp">namespace Enum.Extensions
{
    public static class EnumerationExtensions
    {
        public static bool Has<T>(this System.Enum type, T value)
        {
            try
            {
                return (((int)(object)type & (int)(object)value) == (int)(object)value);
            }
            catch
            {
                return false;
            }
        }

        public static bool Is<T>(this System.Enum type, T value)
        {
            try
            {
                return (int)(object)type == (int)(object)value;
            }
            catch
            {
                return false;
            }
        }

        public static T Add<T>(this System.Enum type, T value)
        {
            try
            {
                return (T)(object)(((int)(object)type | (int)(object)value));
            }
            catch (Exception ex)
            {
                throw new ArgumentException(
                    string.Format(
                        "无法将值添加到枚举类型 '{0}'。",
                        typeof(T).Name
                        ), ex);
            }
        }

        public static T Remove<T>(this System.Enum type, T value)
        {
            try
            {
                return (T)(object)(((int)(object)type & ~(int)(object)value));
            }
            catch (Exception ex)
            {
                throw new ArgumentException(
                    string.Format(
                        "无法从枚举类型 '{0}' 中移除值。",
                        typeof(T).Name
                        ), ex);
            }
        }
    }
}</code>

These methods can be used like this:

<code class="language-csharp">SomeType value = SomeType.Grapes;
bool isGrapes = value.Is(SomeType.Grapes); // true
bool hasGrapes = value.Has(SomeType.Grapes); // true

value = value.Add(SomeType.Oranges);
value = value.Add(SomeType.Apples);
value = value.Remove(SomeType.Grapes);

bool hasOranges = value.Has(SomeType.Oranges); // true
bool isApples = value.Is(SomeType.Apples); // false
bool hasGrapes = value.Has(SomeType.Grapes); // false</code>

The above is the detailed content of C# Bitwise Enum Operations: A Concise Cheat Sheet?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn