public static IEnumerable<T> CreateEnumerable<T>(params T[] values) => values; //And then use it IEnumerable<string> myStrings = CreateEnumerable("first item", "second item");//etc..
IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. It is the base interface for all non-generic collections that can be enumerated. This works for read-only access to a collection that implements that IEnumerable can be used with a foreach statement.
IEnumerable in C# is an interface that defines one method, GetEnumerator which returns an IEnumerator interface. This allows readonly access to a collection then a collection that implements IEnumerable can be used with a for-each statement.
What you can do is use the Add extension method to create a new IEnumerable<T> with the added value. var items = new string[]{"foo"}; var temp = items; items = items. Add("bar");
Ok, adding to the answers stated you might be also looking for
IEnumerable<string> m_oEnum = Enumerable.Empty<string>();
or
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"};
As string[]
implements IEnumerable
IEnumerable<string> m_oEnum = new 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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With