2

I have a method such as:

public void DoIt(params MyEnum[] channels)
{

}

Is there a way to get the int values of the enums in the params?

I tried var myInts = channels.Select(x => x.Value) but no luck

Jon
  • 38,814
  • 81
  • 233
  • 382

7 Answers7

7
var myInts = channels.Cast<int>();
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
7
var ints = (int[]) (object) channels;
Zakharia Stanley
  • 1,196
  • 1
  • 9
  • 10
5

You can do a cast in the select clause:

var myInts = channels.Select(x => (int)x);
Matten
  • 17,365
  • 2
  • 42
  • 64
5

Another way to do the same, without LINQ:

var vals = Array.ConvertAll(channels, c =>(int)c);
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

var myInts = channels.Select(x=x.Value) this doesn't work because = needs to be => (int)x

I think var myInts = channels.Select(x => (int)x).ToArray(); will do what you want.

evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
1

This should do it:

public void DoIt(params MyEnum[] channels)
{
   int[] ret = channels.Select(x => ((int)x)).ToArray();
}
gideon
  • 19,329
  • 11
  • 72
  • 113
1

You could do:

var myInts = channels.Select(e => (int)e);

Or as a LINQ query:

var myInts = from e in channels select (int)e;

You could then call ToArray() or ToList() on myInts to get a MyEnum[] or a List<MyEnum>

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326