6

the following lambda statemement returns null, when i was hoping it would return a string value.

var countryCode = AddressComponents
    .Where(x => x.AddressType == AddressType.Country)
    .Select(x => x.ShortName)
    .SingleOrDefault();

now the AddressType property of the current instance i'm interrigating contains the following data:

AddressType.Political | AddressType.Country

so it's containing TWO values.

Of course, my lambda will not work, because the value of country (lets assume it's 1) != the value of Political bitwise or Country (lets assume it's 1 | 2 == 3).

Any ideas?

I'm worried i need to have something really fraking ugly like ...

((AddressTypes & AddressType.Country) == AddressType.Country)

.. thoughts?

Pure.Krome
  • 84,693
  • 113
  • 396
  • 647

2 Answers2

9

.NET 4.0 has theEnum.HasFlagmethod:

x => x.AddressType.HasFlag(AddressType.Country)

If you are not on .NET 4.0, the bitwiseANDyou have there is a good choice. If you don't like the pattern, check out UnconstrainedMelody, which has an extension method for this purpose. Alternatively, you can write one yourself; this question will probably be of help - Anyone know a good workaround for the lack of an enum generic constraint?

Community
  • 1
  • 1
Ani
  • 111,048
  • 26
  • 262
  • 307
  • SERIOUS?! /brb .... HAHAHAH frak me :) It does too! Win. win and el-winno-win!!! What about if u want to check for TWO flags. eg AddressType.Country or AddressType.Foo ? can HasFlag(AddressType.Country) | AddressType.Foo) work? *(notice the single PIPE character)* – Pure.Krome Oct 07 '10 at 05:34
  • @Pure.Krome: Yes, that should work. From MSDN: "The HasFlag method returns the result of the following Boolean expression: thisInstance And flag = flag" – Ani Oct 07 '10 at 05:38
6

I urge caution with Enum.HasFlag in .NET 4.0. When I profiled my compute-bound app, using ANTS 6.0, this function came up near the top. Going back to the old manual bit flag testing gave a factor of >100 speedup in my case. Until this BCL bug is worked out, consider yourself advised:

using System;
using System.Diagnostics;

class Program
{
    [Flags] enum test { flag1 = 1, flag2 = 2, flag4 = 4 }

    static void Main(string[] args)
    {
        Stopwatch s;
        test t = test.flag4;

        s = Stopwatch.StartNew();
        for (int c=0,i=0; i < 50000000; i++)
            if (t.HasFlag(test.flag2))
                c++;
        Console.WriteLine(s.ElapsedMilliseconds);   // 22837 ms.

        s = Stopwatch.StartNew();
        for (int c=0,i=0; i < 50000000; i++)
            if ((t & test.flag2) > 0)
                c++;
        Console.WriteLine(s.ElapsedMilliseconds);   // 172 ms.
    }
}
Glenn Slayden
  • 17,543
  • 3
  • 114
  • 108
  • Sadly this is true, well known, and explained here - http://www.codeproject.com/Tips/441086/NETs-Enum-HasFlag-and-performance-costs – Stephen Kennedy Jun 26 '13 at 17:22