How this enum is assigned? What are all the value for each?
public enum SiteRoles
{
User = 1 << 0,
Admin = 1 << 1,
Helpdesk = 1 << 2
}
What is the use of assigning like this?
Used in this post
They're making a bit flag. Instead of writing the values as 1, 2, 4, 8, 16, etc., they left shift the 1
value to multiply it by 2. One could argue that it's easier to read.
It allows bitwise operations on the enum value.
1 << 0 = 1 (binary 0001)
1 << 1 = 2 (binary 0010)
1 << 2 = 4 (binary 0100)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With