Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define enums within a method in C#?

I have mainly a C++ background and I am learning C#. So, I need some help with C# idioms and style.

I am trying to write, in C#, a small text-file parsing method in which I need a simple state variable with three states. In C++ I would declare an enum like this for the state variable:

enum { stHeader, stBody, stFooter} state = stBody; 

...and then use it in my parsing loop like this:

if (state == stHeader && input == ".endheader") {   state = stBody; } 

In C# I realize that it is not possible to declare an enum inside a method. So, what I am supposed to do for sake of clean style? Declare this internal enum outside of the method? Use magic numbers 1,2,3? Create a separate class for this?

Please help me clear up my confusion.

like image 589
danatel Avatar asked Apr 09 '11 08:04

danatel


People also ask

Can we define enum inside a method?

We can an enumeration inside a class. But, we cannot define an enum inside a method. If you try to do so it generates a compile time error saying “enum types must not be local”.

Can we define enum inside a method in C#?

You cannot define a Class, Enum, or Struct inside a method; that's just the way the C# language is implemented.

Can you define an enum within a class?

Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.

Can we have enum inside enum in C?

It is not possible to have enum of enums, but you could represent your data by having the type and cause separately either as part of a struct or allocating certain bits for each of the fields.

What is enum in C programming?

Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.

What is enumeration in C language?

Enumeration is a user defined datatype in C language. It is used to assign names to the integral constants which makes a program easy to read and maintain. The keyword “enum” is used to declare an enumeration. enum enum_name {const1, const2, .......

What are the values of the enum constants?

// The name of enumeration is "flag" and the constant // are the values of the flag. By default, the values // of the constants are as follows: // constant1 = 0, constant2 = 1, constant3 = 2 and // so on. enum flag {constant1, constant2, constant3, .......

How to define the variables of enum type in Java?

There are two ways to define the variables of enum type as follows. In the above program, two enums are declared as week and day outside the main () function. In the main () function, the values of enum elements are printed.


2 Answers

The closest you can get is a private nested enum with in the class:

public class TheClass {     private enum TheEnum      {        stHeader,         stBody,         stFooter     }      // ...the rest of the methods properties etc...  } 
like image 142
Willem van Rumpt Avatar answered Sep 23 '22 01:09

Willem van Rumpt


You can also use the constant variables but I prefer and I think is better code style to use Enums

 public class Class1 {     private enum TheEnum     {         stHeader,         stBody,         stFooter     }     public void SomeMethodEnum()     {         TheEnum state = TheEnum.stBody;         switch (state)         {             case TheEnum.stHeader:                 //do something                 break;             case TheEnum.stBody:                 break;             case TheEnum.stFooter:                 break;             default:                 throw new ArgumentOutOfRangeException();         }     }       public void SomeMethodConst()     {         int state = 1;         const int Header = 1;         const int Body = 2;         const int Footer = 3;          switch (state)         {             case Header:                 break;             case Body:                 break;             case Footer:                 break;             default:                 throw new ArgumentOutOfRangeException();         }      } } 
like image 39
Serghei Avatar answered Sep 25 '22 01:09

Serghei