Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5968079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:02:17+00:00 2026-05-22T20:02:17+00:00

Looking for a code optimization in c# that allows me to both define an

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:02:18+00:00Added an answer on May 22, 2026 at 8:02 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm interested in speed, not good looking code, that is why I'm using array
I am testing some weird-looking CSS code that I wrote (I'm using a mix
Looking at some code I'm maintaining in System Verilog I see some signals that
While looking at a micro-optimization question that I asked yesterday ( here ), I
I have some old school looking code that is as follows: IList<KeyValuePair<string, ValuePair>> ServicePairs
Looking for code samples to access a memory map file in C#
I was looking at code in WPF which is in VB. I am trying
I have a piece of code looking like this : TAxis *axis = 0;
Looking to improve my IF statement, and I want to keep my code looking
Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.