0

I have an enum defined as

public enum SecurityRole { Admin = 0, ViewLog = 1, DeleteLog = 2 } 

and want to use the technique described here to get a List<SecurityRole>.

Thus the line

var lst = Enum.GetValues(typeof(SecurityRole)).Cast<SecurityRole>().ToList(); 

should work, but IntelliSense wont allow the .ToList property

I am using System.Linq. Is there some other reference I need?

3
  • 1
    Your code is valid, and executes successfully. As a shot in the dark, maybe Visual Studio is confused? Are there compilation errors, or just IntelliSense failures? Try relaunching VS. Commented Mar 17, 2013 at 6:45
  • @kirsten it works for me and this is the correct syntax, see the official question on this topic: stackoverflow.com/questions/1167361/… - Can you delete the . before ToList() and press the . if that fails press Ctrl+Space to bring up intellisense Commented Mar 17, 2013 at 7:07
  • just try to rebuild the solution and try again because ur code should work since you use: using System.Linq Commented Mar 17, 2013 at 7:43

2 Answers 2

2

using System.Linq should do it. ToList() is an IEnumerable<T> extension method. Probably check if you have any other compilation errors in your code.

Sign up to request clarification or add additional context in comments.

2 Comments

Cast<T> is also defined in Enumerable class
There were compiler errors. It works now they have been resolved.
2

Check the solution here: How do I convert an enum to a list in C#?

Enum.GetValues(typeof(SomeEnum)).Cast<SomeEnum>(); 

1 Comment

That will return an IEnumerable<SomeEnum>. To return a List<SomeEnum>, answers there recommend calling ToList(), just as the OP here has done.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.