return a IEnumerable<string> ( codewars assignment) | Sololearn: Learn to code for FREE!

+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/6/2019 9:00:01 PM

sneeze

10 Answers

New Answer

+2

https://code.sololearn.com/c9o817SSJvTc/?ref=app

+2

try (output as List<string>).Add(""); or ((List<string>) output).Add(""); or if (output is List<string>) { ((List<string>) output).Add(""); }

+2

When an instance reference has an interface type, you can use all the methods defined in the interface. But to use methods that are not related to interface methods, you need to explicitly cast the type of the reference to the class type of the object. But you need to be careful ... I will give an example

+1

IEnumerable<T> is an interface implemented by the List<T> class. Therefore, you can also do this: IEnumerable<string> refer = new List<string>(); (refer as List<string>).Add("Some string"); Additionally, you are familiar with LINQ extension methods?

+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

Use extension method IEnumerable<T>: ForEach() output.ForEach(Console.WriteLine); or loop: foreach (var item in output) { Console.WriteLine(item); }

+1

I highly recommend practicing all methods. This is for C# - must have https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1?view=netframework-4.7.2#extension-methods

+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!!