1

A base class have readonly field of type List<SomeEnum> which the derived classes will initialize. Now, there is a derived class in which I want to add the all the values of the SomeEnum. One way is to type all the enum values, but the enum is a little big, so is there any other way to do it?

public class Base
{
 private readonly List<SomeEnum> _list;

 protected Base(List<SomeEnum> list)
 {
  _list = list;
 }
}

public class Derived : Base
{
 public Derived() : base(new List<SomeEnum>() { Enum.GetValues(typeof(SomeEnum)) }
 {
 }
}

(The above code would not compile, I believe intializers don't accept arrays.)

1
  • 1
    You may want to accept IEnumerable<SomeEnum> or IList<SomeEnum> in the base constructor for future flexibility. Commented Jan 7, 2011 at 7:34

5 Answers 5

4

It's because you have to cast the result of Enum.GetValues to the appropriate IEnumerable<T> type. See here:

using System.Linq;


public class Derived : Base
{
    public Derived()
        : base(new List<SomeEnum>(Enum.GetValues(typeof(SomeEnum)).OfType<SomeEnum>()))
    {
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

You have to import System.Linq.
2

As Mark wrote, there is a List constructor that accepts an enumerable, maybe you can leverage it the following (less coupled) way:

public class Base 
{     
  private readonly List<UserActions> _list;      

  protected Base(IEnumerable<UserActions> values)
  {         _list = new List<UserActions>(values);  
  } 
}  

public class Derived : Base 
{     
   public Derived() : base((UserActions[]) Enum.GetValues(typeof(UserActions))
  { 
  }
} 

Comments

2

That won't work because the collection initializer calls Add with your argument, and you can't add an Array to a List using the Add method (you'd need AddRange instead).

However there is a constructor for List<T> can accept IEnumerable<T>. Try this:

new List<SomeEnum>((IEnumerable<SomeEnum>)Enum.GetValues(typeof(SomeEnum)))

2 Comments

This does not compile. Enum.GetValues returns an Array, which does not implement IEnumerable<T>. It is not a generic.
I don't know why this answer is being voted up. It doesn't work. See my answer for one that does work.
1

Did you tried passing the array of values in the constructor of the list, like this:

public Derived() : 
         base(new List<SomeEnum>(Enum.GetValues(typeof(UserActions))) 
{}

2 Comments

Arrays do not implement IEnumerable. So the code doesn't compiles.
@hab: Actually arrays do implement IEnumerable.
1
base(new List<SomeEnum>(((IEnumerable<SomeEnum>)Enum.GetValues(typeof(SomeEnum)).GetEnumerator())))

I got it working by casting the enumerator returned to the generic type (which List accepts.)

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.