Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enum with value 0x0001?

Tags:

c#

enums

I have an enum declaration like this:

public enum Filter
{
  a = 0x0001;
  b = 0x0002;
}

What does that mean? They are using this to filter an array.

like image 619
Broken Link Avatar asked Jul 23 '09 18:07

Broken Link


2 Answers

It means they're the integer values assigned to those names. Enums are basically just named numbers. You can cast between the underlying type of an enum and the enum value.

For example:

public enum Colour
{
    Red = 1,
    Blue = 2,
    Green = 3
}

Colour green = (Colour) 3;
int three = (int) Colour.Green;

By default an enum's underlying type is int, but you can use any of byte, sbyte, short, ushort, int, uint, long or ulong:

public enum BigEnum : long
{
    BigValue = 0x5000000000 // Couldn't fit this in an int
}
like image 90
Jon Skeet Avatar answered Nov 16 '22 12:11

Jon Skeet


It just means that if you do Filter->a, you get 1. Filter->b is 2.

The weird hex notation is just that, notation.

EDIT: Since this is a 'filter' the hex notation makes a little more sense.

By writing 0x1, you specify the following bit pattern:

0000 0001

And 0x2 is:

0000 0010

This makes it clearer on how to use a filter.

So for example, if you wanted to filter out data that has the lower 2 bits set, you could do:

Filter->a | Filter->b

which would correspond to:

0000 0011

The hex notation makes the concept of a filter clearer (for some people). For example, it's relatively easy to figure out the binary of 0x83F0 by looking at it, but much more difficult for 33776 (the same number in base 10).

like image 9
samoz Avatar answered Nov 16 '22 13:11

samoz