Useful thingy
-
I have just noticed that in the .NET 4.0 the Enum.HasFlag(Enum)[^] method was introduced. Not much clever from my side, but I rarely put a dot after enum's member so I could notice the new feature. :thumbsup:
Greetings - Jacek
-
[deleted - made a boo boo] :doh:
Dave Find Me On: Web|Facebook|Twitter|LinkedIn
Folding Stats: Team CodeProject
modified on Thursday, July 7, 2011 3:15 PM
-
This is not how HasFlag works... But of course it is easy to recreate with a simple AND.
Greetings - Jacek
-
I have just noticed that in the .NET 4.0 the Enum.HasFlag(Enum)[^] method was introduced. Not much clever from my side, but I rarely put a dot after enum's member so I could notice the new feature. :thumbsup:
Greetings - Jacek
Hi Jacek, Interesting, I thought I had been using 'HasFlag on the MouseButtons Enum in Windows Forms previous to .NET 4.0. Also interesting about 'HasFlag is that it will work even on Enumerations that are not decorated with the [Flags] Attribute, while the MS documentation implies, in my reading, that it only works with same: "The HasFlag method is designed to be used with enumeration types that are marked with the FlagsAttribute attribute."
// from the MS doc for Enum.HasFlag // no \[Flags\] attribute ! public enum DinnerItems { None = 0, Entree = 1, Appetizer = 2, Side = 4, Dessert = 8, Beverage = 16, BarBeverage = 32 } // execute this somewhere in your code DinnerItems thisMeal; // This will also compile without error in .NET 4.0 ! // DinnerItems thisMeal = new DinnerItems(); thisMeal = DinnerItems.Dessert; if (thisMeal == DinnerItems.Dessert) MessageBox.Show("woof woof"); if(thisMeal.HasFlag(DinnerItems.Dessert)) MessageBox.Show("meow");
The comment on the MSDN page you linked to by a "community member" that using 'HasFlag is "1000 times slower" than the "standard method" is kind of scary. best, Bill
"Reason is the natural order of truth; but imagination is the organ of meaning." C.S. Lewis