Checking UInt16 for flags using Enum.HasFlag()?

I have a C#/.NET 4.0 application that receives and parses some data from a network connection. One particular piece of data that I need to parse is a UInt16 value that represents a set of flags. I've defined an enum that represents these flags like this:

public enum Fields
{
    None = 0,
    FirstFlag = 1,
    SecondFlag = 2,
    ThirdFlag = 4,
    FourthFlag = 8,
    FifthFlag = 16,
    SixthFlag = 32,
    SeventhFlag = 64,
    EighthFlag = 128
}

I going to try to use bitwise operators on this UInt16 value to determine which flags it indicated had been set. But then I ran across Enum.HasFlag() method and at first glance, that seemed like it would be easier than bitwise operations. I've been trying to use the following method to determine which flags are set in the UInt16 value:

public static void CheckForSetFields(Fields value)
{
    if (value.HasFlag(Fields.None))
    {
        System.Diagnostics.Debug.WriteLine("No fields are set!");
    }
    else
    {
        if (value.HasFlag(Fields.FirstFlag))
        {
            System.Diagnostics.Debug.WriteLine("First flag is set.");
        }
        if (value.HasFlag(Fields.SecondFlag))
        {
            System.Diagnostics.Debug.WriteLine("Second flag is set.");
        }

        // etc., etc.

        if (value.HasFlag(Fields.EighthFlag))
        {
            System.Diagnostics.Debug.WriteLine("Eighth flag is set.");
        }
    }
}

This method obviously takes a parameter of type Fields (my enum) but I'm working with a UInt16 value that was sent to me. I assume that I need to convert the UInt16 into an instance of the Fields enum but I'm not sure how to do that. It doesn't seem to be as simple as just casting it (which doesn't give me an error but also doesn't return the expected results, either). Is it possible to do this using Enum.HasFlag() or should I just implement the bitwise operations and be done with it?

Jon Skeet
people
quotationmark

I assume that I need to convert the UInt16 into an instance of the Fields enum but I'm not sure how to do that.

That's easy - you need to cast to the underlying type of the enum, and then you can cast to the enum itself:

Fields value = (Fields) (int) incomingValue;

EDIT: As noted in comments, this check doesn't do what you want it to:

if (value.HasFlag(Fields.None))

From the documentation:

If the underlying value of flag is zero, the method returns true.

Basically you want:

if (value == 0)

for that test instead.

people

See more on this question at Stackoverflow