Using integer enumeration without casting in C #

Good afternoon let's say i have an enumeration

public enum Test : int { TestValue1 = 0, TestValue2, TestValue3 } 

Why can't I use expressions like Int32 IntTest = Test.TestValue1 without a translation meaning IntTest = 0 ? Would it be helpful if I decided to add more items to the list later? I think I am forced to use Int32 IntTest = (Int32) Test.TestValue1 , which I think should be superfluous ... Also, why can't I do something like

 switch (IntTest) { case (Test.TestValue1) : DoSomething(); break; case (Test.TestValue2) : DoSomethingElse(); break; default : Do Nothing(); } 

? The compiler says it expects a constant value instead of TestValue1 ... Isn't that value constant?

Many thanks.

+6
enums casting c #
source share
9 answers

Please note that you can do this:

 switch ((Test)IntTest) { case (Test.TestValue1) : DoSomething(); break; case (Test.TestValue2) : DoSomethingElse(); break; default : DoNothing(); } 

Casting to a test from int is guaranteed not to fail, even if the value is not in the enumeration. If this value is not in the enumeration, a call to ToString () of the Test instance will return a string representation of the base numeric value.

+8
source share

You cannot use Int32 and Test interchangeably because they are semantically different types, even if they use the same underlying storage engine.

If you need Int32 , use Int32 ; if you need Test , then use Test ; if you need to convert between them, use an explicit cast.

(And, of course, the language could easily be resolved implicitly for these transformations, but such a โ€œfunctionโ€ would probably create more problems than it solved.)

+5
source share

You need to make a throw because you must indicate in the definition of language. Reserve? May be. But the way it is.

For your switch :

 switch (IntTest) { case (int)Test.TestValue1: DoSomething(); break; } 

Enumeration values โ€‹โ€‹must be fully qualified with the name of the enumeration type.

+3
source share

I wanted to implement an enumeration just like this in order to simplify the conversion of an application with table column names as rows to indexes.

This is what I am trying to achieve:

 enum TableName { Column1Name = 0 } Column1Data = (cast)objData[RowIndex][TableName.Column1Name]; 

the problem mentioned above is that C # requires an explicit cast when using enum's

 Column1Data = (cast)objData[RowIndex][(int)TableName.Column1Name]; //what a waste of time.. 

But I have a workaround .. groans all you want: P (this is not a production application - please think about the impact of this code before implementing it in a live environment)

 public static class TableName { public const int Column1Name = 0; } Column1Data = (cast)objData[RowIndex][TableName.Column1Name]; //this is now valid, and my sanity remains intact - as does the integer value ;) 
+2
source share

enums works as a design
As msdn says:

The main type indicates how many for each enumerator. However, an explicit cast must be converted from an enumeration type to an integral type.

+1
source share

Hey hey, I just read this chapter in my book.

First of all, I would like to mention that you are trying to do this

 Console.WriteLine(Test.TestValue1); 

You will notice something.

The output is not a number or even an integer.

In addition, this should also answer your second question.

Therefore, you must explicitly use your enum for an integer every time.

:)

+1
source share

As Paul pointed out, you have a syntax error that should resolve (which seems to be fixed) . As another Test pointed out, this is an enumeration that contains integer values. For example, an enumeration can be used to process values โ€‹โ€‹such as x0001 or 0xA0BC (41148).

In the example, you used Int32 IntTest , which is a 32-bit integer; I can add that you should use int instead of Int32 , and if your enum does not hold something else, then the integer will not even indicate : int since the default hold type is really integer.

Regarding the reason the compiler forces you to use a value; Its just how it works, the enumeration is NOT an integer, only the values โ€‹โ€‹of its possible sub-properties (TestValue1, TestValue2, TestValue3) are integers. *

As someone pointed out, what will be printed in Console.WriteLine is not an integer. *

+1
source share
 public enum Test { TestValue1 = 0, TestValue2, TestValue3 } 

In a method, you can use an enumeration as a Type.
Just include a variable of type Test, then you do not need to enter an enumeration in int.

 public void DoSwitch(Test val) { switch (val) { case (Test.TestValue1) : DoSomething(); break; case (Test.TestValue2) : DoSomethingElse(); break; default : Do Nothing(); } } 
+1
source share
  • First, the : int part of your enumeration is completely redundant, since int already the default control type for the enumeration.

  • A cast is required because when you write an enumeration, you are actually defining the new Type as a class. Any value of an enumeration named Test is of type Test .

0
source share

All Articles