Macro for enum values

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

I'm porting a piece of C++ code to C#. It looks something like this:

#define MAKE_ENUM(x, y, z) (((x) << 16) | ((y) << 8)) | (z))

enum MyEnum
{
A = MAKE_ENUM(3, 9, 0),
B = MAKE_ENUM(4, 10, 1),
C = MAKE_ENUM(5, 11, 2),
};

You get the idea. Now can I do this sort of thing in C#? I haven't had much luck
making the macro into a function for obvious reasons.
 
Bob,

No, you can not. Macros are not supported in C#.

However, there is nothing stopping you from doing:

enum MyEnum
{
A = (((3) << 16) | ((9) << 8)) | (0)),
B = (((4) << 16) | ((10) << 8)) | (1)),
C = (((5) << 16) | ((11) << 8)) | (2))
};

Of course, readability takes a big hit here. I don't see why you just
don't use the constant values. I mean, the MAKE_ENUM macro (if supported)
isn't incredibly obvious either.

Hope this helps.
 
Back
Top