8

Looking for a code optimization in c# that allows me to both define an enum and create a variable of that enum's type simultaniously:

Before:

    enum State {State1, State2, State3};
    State state = State.State1;

After (doesn't work):

    enum State {State1, State2, State3} state;
    state = State.State1;

Does anything like that exist?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Dave
  • 83
  • 1
  • 1
  • 3
  • Even if there were such a way, it wouldn't be any more optimized. `enum State {State1, State2, State3}` doesn't cost anything at run-time. – Allison Lock May 30 '11 at 14:59

5 Answers5

8

There's no support for that in C#, but maybe you can workaround this and do something "near" to an enum by using tuples or anonymous types if you only need to switch some state and you don't need to do any special operations with it.

For example, using tuples, you can do this:

var someFakeEnum = Tuple.Create(0, 1);

UPDATE:

C# 7 has introduced syntactic tuples:

var someFakeEnum = (State1: 0, State2: 1);

Or with anonymous types:

var someFakeEnum = new { State1 = 0, State2 = 1 };

And, after that, you can do something like:

int state = 0;

// If you're using tuples...
if(some condition)
{
    state = someFakeEnum.Item2;
}

// ...or if you're using anonymous types or C# >= 7 tuples...
if(some condition) 
{
    state = someFakeEnum.State2;
}

Obviously this isn't an actual enumeration and you don't have the sugar that Enum type provides for you, but you can still use binary operators like OR, AND or conditionals like any other actual enumeration.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
6

This is not an optimization.

No, nothing like that exists, and for good reason. It's much less readable and there's absolutely zero benefit to be gained in doing so. Declare them in two separate statements and be done with it.

If you're literally getting paid to reduce the number of lines in your source code, write it like this:

enum State {State1, State2, State 3}; State state = State.State1;

(Yes, that's a joke. Mostly.)

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 4
    I wonder how much horrible code I indirectly produced by being sarcastic in my advices to beginner programmers.. – Dyppl May 30 '11 at 15:06
  • 3
    Inline enums would actually be useful when the use of enum is very limited for example for defining method parameters, where it accepts only few states and there's really no use to have a dedicated enum type in your namespace. This can be realized better in C# 6.0 that simplifies use of static members. – orad Jan 29 '15 at 00:07
3

Switching from C/C++, I suppose?

No, can't do that

Dyppl
  • 12,161
  • 9
  • 47
  • 68
1

No, that doesnt exists. Nor is it very readable IMHO

TJHeuvel
  • 12,403
  • 4
  • 37
  • 46
0

No, in C# the declaration or a type and using that type to declare variables are separate.

In C/C++ it's common to declare and use a type directly, for example with the struct keyword. In C# they are separate.

Note that in C# you don't need the semicolon after declaring an enum (or a class or a struct):

enum State { State1, State2, State3 }

It's still allowed though, presumably to be a little more compatible with C/C++ syntax.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005