Macro for enum values

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top