Sobes.tech
Junior — Middle

Can you explain what enum flags mean and how to use them in programming?

sobes.tech AI

Answer from AI

Flags Enum in C# are enumerations that allow combining multiple values using bitwise operations. For this, the [Flags] attribute is applied to the enumeration. Each Enum element is usually set as a power of two (1, 2, 4, 8, etc.) so that values can be combined using bitwise OR (|).

Example of usage:

[Flags]
public enum FileAccess
{
    Read = 1,       // 0001
    Write = 2,      // 0010
    Execute = 4     // 0100
}

// Combining flags
FileAccess permissions = FileAccess.Read | FileAccess.Write;

// Checking for a flag
bool canRead = (permissions & FileAccess.Read) == FileAccess.Read;

This is convenient for storing and checking multiple boolean options in a single value.

Can you explain what enum flags mean and how to use… - sobes.tech