I love this Flag bit feature. Ever since I discover this, I always try to use it whenever it fits the situation like for a “Status” column, which before knowing this I just use plain integer. Anyway, I use power of 2 (1, 2, 4, 8, etc) just cuz it works lol…. Nice video sir as always
@IAmTimCoreyАй бұрын
Thanks for sharing!
@farzadmfАй бұрын
Question: what's the point of [Flags] attribute if we need to manually set them "by hand" ourselves? I thought it should somehow take care of setting them to proper binary values based on their order
@Kar08MieАй бұрын
yes, i'm also surprised .NET doesnt manage this automatically for us.
@IAmTimCoreyАй бұрын
The Flags attribute allows it to be treated as a binary flag rather than an int. That means you can do bitwise operations on the values. It also means that when you do a ToString, it outputs the flag values instead of an int, so “Read, Write” instead of 3. Remember too that you can have an enum variable that has an integer value that isn’t one of the enum options (since it is a combination of two or more values.
@farzadmfАй бұрын
@@IAmTimCorey Thanks for the explanation
@camerontangen2957Ай бұрын
Literally woke up this morning realizing I need to figure out how to use Flags in my own personal project. Thank you!
@IAmTimCoreyАй бұрын
I am glad it was helpful.
@randlmorales4890Ай бұрын
What is the highest number of enum values you can use until it becomes better to use a different approach?
@IAmTimCoreyАй бұрын
You can technically make the enum a long (64 bit), but that’s not really the issue. The issue is practicality. If you get into the 20-item range, you should probably be thinking through your design. It might be time to refactor.
@Ultimate_LukiАй бұрын
Thanks for the video. You should do one for how to check whether a flag is set and how to set it and how to toggle it. For example if you use userAccountControl from AD and a user has a value of 2080, how to check whether its enabled.
@IAmTimCoreyАй бұрын
These are in the queue.
@princefowzanАй бұрын
so putting [Flags] before the enum makes it a flag, here, what is this flags?, a class, a type or what? what is this syntax ?
@halbertwalstonАй бұрын
In this usage, "[Flags]" is an Attribute of the enumerator (System.Attributes).
@krccmsitp2884Ай бұрын
Instead of shifting (1
@IAmTimCoreyАй бұрын
Personal preference is fine. For me, I prefer the shorter syntax rather than trying to read the number of zeros.