Initializing IEnumerable <string> In C #
I have this object:
IEnumerable<string> m_oEnum = null; and I would like to initialize it. I tried
IEnumerable<string> m_oEnum = new IEnumerable<string>() { "1", "2", "3"}; but he says: βIEnumerable does not contain a method to add a string. Any idea? Thanks
+76
markzzz Jul 04 2018-11-14T00: 00Z
source share6 answers
Well, by adding to the answers you can also search
IEnumerable<string> m_oEnum = Enumerable.Empty<string>(); or
IEnumerable<string> m_oEnum = new string[]{}; +124
sehe Jul 04 2018-11-14T00: 00Z
source shareIEnumerable<T> is the interface. You need to start with a specific type (which implements IEnumerable<T> ). Example:
IEnumerable<string> m_oEnum = new List<string>() { "1", "2", "3"}; +69
Bas Jul 04 2018-11-11T00: 00Z
source shareHow string[] implements IEnumerable
IEnumerable<string> m_oEnum = new string[] {"1","2","3"} +21
Bob Vale Jul 04 2018-11-11T00: 00Z
source shareIEnumerable is just an interface and therefore cannot be directly created.
You need to create a specific class (e.g. List )
IEnumerable<string> m_oEnum = new List<string>() { "1", "2", "3" }; you can pass this on to anything IEnumerable expects.
+14
ChrisF Jul 04 2018-11-11T00: 00Z
source share public static IEnumerable<string> GetData() { yield return "1"; yield return "2"; yield return "3"; } IEnumerable<string> m_oEnum = GetData(); +8
Kirill Polishchuk Jul 04 2018-11-11T00: 00Z
source shareYou cannot instantiate an interface β you must provide a concrete implementation of IEnumerable.
+4
BonyT Jul 04 2018-11-11T00: 00Z
source share