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.
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”.
You cannot define a Class, Enum, or Struct inside a method; that's just the way the C# language is implemented.
Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.
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.
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.
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, .......
// 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, .......
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.
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... }
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(); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With