Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
and I'd like to initialize it. Tried with
IEnumerable<string> m_oEnum = new IEnumerable<string>() { "1", "2", "3"};
but it say "IEnumerable doesnt contain a method for add string. Any idea? Thanks
Ok, adding to the answers stated you might be also looking for
IEnumerable<string> m_oEnum = Enumerable.Empty<string>();
IEnumerable<string> m_oEnum = new string[]{};
–
–
IEnumerable<T> is an interface. You need to initiate with a concrete type (that implements IEnumerable<T>). Example:
IEnumerable<string> m_oEnum = new List<string>() { "1", "2", "3"};
IEnumerable is just an interface and so can't be instantiated directly.
You need to create a concrete class (like a List)
IEnumerable<string> m_oEnum = new List<string>() { "1", "2", "3" };
you can then pass this to anything expecting an IEnumerable.
IEnumerable is an interface, instead of looking for how to create an interface instance, create an implementation that matches the interface: create a list or an array.
IEnumerable<string> myStrings = new [] { "first item", "second item" };
IEnumerable<string> myStrings = new List<string> { "first item", "second item" };
You can create a static method that will return desired IEnumerable like this :
public static IEnumerable<T> CreateEnumerable<T>(params T[] values) =>
values;
//And then use it
IEnumerable<string> myStrings = CreateEnumerable("first item", "second item");//etc..
Alternatively just do :
IEnumerable<string> myStrings = new []{ "first item", "second item"};
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.