Nicer way to check for a flag presence

by Miha Markič 23. December 2007 01:26

Imagine you have this enum definition and a variable of same type:

[Flags] public enum Tubo { One, Two } ... Tubo tubo = Tubo.One;

Now, how do you check if a variable of type Tubo contains a flag, i.e. Tubo.One? Simple, like this:

bool b = (tubo & Tubo.One) == Tubo.One;

But do you really like this notation? I mean it is a lot of typing and the expression is not clear at first sight. That's why I checked out if extension methods might help. So I created this experimental method:

public static bool Contains<T>(this T list, T flag) where T : struct { return (Convert.ToInt32(list) & Convert.ToInt32(flag)) == Convert.ToInt32(flag); }

And the new test would look like this:

bool b = tubo.Contains(Tubo.One);

Isn't this notation much more readable and easy to type? Sure. But there are two drawbacks in here:

  1. If a non-numeric structure is used the compiler won't catch the error (the restriction of generic type T is struct) - at the runtime you'll get casting error when the structure can't be converted to Int32.
  2. Performance. There is a monstrous performance hit, like 500x slower. There are many operations involved - mostly because of Convert.ToInt32 method usage. (extension method call is probably inlined). There is no other way since, as far as Contains method knows, the arguments are struct types, and struct types can't be used like numbers just like that.IOW this code won't work:
  3. public static bool Contains<T>(this T list, T flag) where T : struct { return (int)list & (int)flag == (int)flag; }

    But if the code above worked one would get a decent performance, not much slower than doing a simple bitwise and operation.
Unfortunately there is no better solution AFAIK. It would certainly help if we could restrict generic type T to enum (where T: enum).

Tags:

.net 3.5

Comments (2) -

Karel
Karel
1/2/2008 3:41:48 PM #

Very nice! I love extension methods. Manipulation with Flags enum is one of the biggest inconvenience in .NET.

Do you know this trick which enables extension methods for NET 2.0? Awesome...
feeds.feedburner.com/.../C25.aspx

Reply

Miha Markic
Miha Markic
1/2/2008 5:57:42 PM #

Hi Karel,

Yes, I knew that one. (I am subscribed to Dustin's excellent blog)

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Miha Markic

About me
Righthand
 
Microsoft MVP
 
Developer Express' DXSquad
INETA Country Leader for Slovenia
INETA Country Leader for Slovenia

Slovene Developer Users Group Lead
Friends of Red-Gate
LLBLGenPro Partner

Miha currently works as a free lance consultant and software developer specialized in .net area.
He graduated in Computer and information science at the University of Ljubljana, Slovenia. He has accumulated experience in various programming languages such as Java, Visual Basic 3-6 (MCP), Visual C++, Delphi, C# and VB.Net through years.
He has experience in practically all (technical) stages of project development, including planning, framework development, user interface, business processes, as well as testing and documenting. He has worked on big and small projects in Slovenia and abroad (e.g. participated in completing level 3 IS for the Nucor steel plant, Hertford, USA).
Currently he enjoys programming in .net environment using C#. Since 2000 he has been active in Developer Express' DX Squad and has been ECDL trainer and tester. He also gives lectures on conferences and other events in Slovenia.

Month List

Tag cloud

Most comments

Paulius Paulius
1 comments
us United States
Meh Meh
1 comments
us United States
bart dm bart dm
1 comments
nl Netherlands

RecentComments

Comment RSS