2

In trying to convert an enumeration into an int[] of its values, I initially came up with the long-winded:

Enum.GetValues(typeof(Isolations.Enumerations.TypeSelectionScope))
.Cast<Isolations.Enumerations.TypeSelectionScope>().Select(t => (int)t).ToArray()

It works, but.. well, it ain't pretty.

I did some searching, and found people using ConvertAll() for this purpose, over multiple lines. So I tried it inline.

Array.ConvertAll(Enum.GetValues(typeof(Isolations.Enumerations.TypeSelectionScope)), value => (int)value)

This generates a type error, and adding some casting within is going to make it as messy as the first attempt.

I also tried a direct cast to an int[] to no avail:

Enum.GetValues(typeof(Isolations.Enumerations.TypeSelectionScope)).Cast<int[]>()

Adding a ToArray() onto this fails also.

So, what's a single-line solution to this that isn't messy?

1 Answer 1

5

Enumerable.Cast<T> iterates through a collection and casts each item to T

You're trying to cast each instance to int[] - you want to cast them to int instead:

.Cast<int>()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.