+ 1
return a IEnumerable<string> ( codewars assignment)
I started to do some codewars assignments. They are fun, but I have diffuculty understanding what they want. The return of the method should be. IEnumerable<string> I like to use a list, and use the "add" method to append data What are examples of IEnumerable<string> ? Can I add data to a IEnumerable<string> ? Can I use a list and convert it to a IEnumerable<string> ?
4 Answers
+ 1
No, I am trying to get better in it.
Unfortunately codewars has assignment but no explanation.
if I use
IEnumerable<string> output = new List<string>();
I get the error
CS1061: 'IEnumerable<string>' does not contain a definition for 'Add'
'IEnumerable<string>' does not contain a definition for 'Add'
Which is right because the type of the object is IEnumerable
I am very confused with codewars it looks like the constantly want array's where I am used to use List's
+ 1
ok very confused not but I have passed the test.
if I a return List<string> output = new List<string>();
it is a correct type (IEnumerable<string>)
If i want to write the content of the list to the console,
System.Console.WriteLine(output);
System.Collections.Generic.List`1[System.String]
How can I write the content of a list to the console ?
+ 1
Thank you Basil. It helps me a lot.
In addition to all what you have said.
From this link
https://stackoverflow.com/questions/4180767/whats-the-difference-between-liststring-and-ienumerablestring
A List<string> is a concrete implementation of IEnumerable<string>. The difference is that IEnumerable<string> is merely a sequence of string but a List<string> is indexable by an int index, can be added to and removed from and have items inserted at a particular index.
Basically, the interface IEnumerable<string> lets you stream the string in the sequence but List<string> lets you do this as well as modify and access the items in the lists in specific ways. An IEnumerable<string> is general sequence of string that can be iterated but doesn't allow random access. A List<string> is a specific random-access variable-size collection.
+ 1
Made a some code, to not forget, how to print a list.
https://code.sololearn.com/cUgkGo21k19K
Thank you!!