1

Given the following enum flag

[Flags]
public enum Options : long
{
    None = 0,
    One = 1 << 0,
    Two = 1 << 1,
    Three = 1 << 2,
    Four = 1 << 3
}

I can create a variable representing a combination of Options and this is represented by a numeric value under the covers (in this case 13)

var oneThreeAndFour = Options.One | Options.Three | Options.Four;

Console.WriteLine(oneThreeAndFour); // One, Three, Four
Console.WriteLine((long) oneThreeAndFour); // 13

I can also convert from the numeric value (in this case 13) back to then combination of Options

Console.WriteLine((Options) 13); // One, Three, Four

My question is how is this conversion happening under the covers? How does .net know that 13 represents One, Three, Four?

The reason I ask is I'm trying the build an equivalent based on a BigInteger that will allow more than 64 values.

The example here: https://stackoverflow.com/a/38557486 works by storing / parsing the actual string value (One, Three, Four) but I would prefer to store / parse the underlying BigInteger value instead.

kimsagro
  • 15,513
  • 17
  • 54
  • 69
  • I think you missed the point inside both `enum` and the `BigFlags` class in your link. Both are just storing the flags as integers. If a bit in the integer is on then the flag is set. Simple as that. In the `BigFlags` class it is ***NOT*** storing / parsing the actual string value. In fact, it is storing the value in `private BigInteger Value;`. – Enigmativity Jul 08 '22 at 00:36
  • If you have a `[Flags] enum` with values `A`, `B`, and `C`, then they equal `1`, `2`, and `4` respectively. So a value of `5` is `1 + 4` (or `101` in binary) and represents `A | C`. Make sense? – Enigmativity Jul 08 '22 at 00:40
  • You're thinking of enums the wrong way around. You can think of an enum as a strong typed `int` / `long`. eg; this `(long)Options.One;` does almost nothing at runtime. In memory, the value was always a `long`, just with some extra / different type information attached. `Enum.HasFlag` is just an integer `&` operation. It's not the value that is changed, it's the `.ToString()` implementation that is different. – Jeremy Lakeman Jul 08 '22 at 01:15
  • This question is wrongly duplicated, or at the very least the duplicate mentioned is not what this question is asking – Zach Hutchins Jul 08 '22 at 01:42
  • @ZachHutchins - The OP is starting with the wrong assumption about the linked answer. Given the correct initial understanding the duplicate is the answer that the OP needs. – Enigmativity Jul 08 '22 at 07:12
  • @Enigmativity That is fair, the question however is more inline with this sentence in my opinion `"My question is how is this conversion happening under the covers? How does .Net know that 13 represents One, Three, Four?"`. Which is a good opportunity to teach and get in depth C# knowledge, and programming knowledge in general, So more of an explanation on why max flag count is a thing. In short the why to the problem not the solution to the problem, or at the very least a reference to a solution to the problem – Zach Hutchins Jul 08 '22 at 16:05

0 Answers0