1

It's possible extract values in enum to Char[]?

I have the Enum:

enum eTypeLanguage { typed = 'T', functional = 'F' }

How to extract values eTypeLanguage to Char array?

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
MeuChapeu
  • 216
  • 1
  • 5
  • 18

2 Answers2

6

Seems you are looking for this (It returns string[]):

Enum.GetNames(typeof(eTypeLanguage));
//returns: typed, functional

If your are looking for values ('T', 'F') then you may do this (this won't throw System.InvalidCastException for your enum):

 var values = Enum.GetValues(typeof(eTypeLanguage))
                  .Cast<eTypeLanguage>()
                  .Select(i => (char)i).ToArray();
 //returns: 'T', 'F'
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
4

If you want to get all enum members' char values, you need to call Enum.GetValues, cast the returned value to the acutal enum type, then cast each member from its enum type to char:

var values = Enum.GetValues(typeof(eTypeLanguage))
                 .Cast<eTypeLanguage>()
                 .Select(e => (char)e)
                 .ToArray();
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • 1
    Oh apologies I missed that bit! still if they have integers it might not work as they expect :) (not that they should).. – Sayse Aug 05 '15 at 11:34
  • @MeuChapeu edited, apparently you need to cast `Enum.GetValues()` response to the actual enum type. – CodeCaster Aug 05 '15 at 12:00