Cannot convert IEnumerable type to list

I have been using C # with Unity3d for several years now, but just starting with .NET programming. I get an error message:

It is not possible to implicitly convert the type ' System.Collections.Generic.IEnumerable<URL> ' to ' System.Collections.Generic.List<URL> '. Explicit conversion exists (are you skipping listing?)

Here is my code:

 namespace TestBrowserHistory { public class Test1 { public Test1() { } static void Main() { InternetExplorer myClass = new InternetExplorer(); List<URL> calledList = myClass.GetHistory(); Console.WriteLine("Hello!"); Console.WriteLine(calledList[1]); Console.ReadLine(); } } } public class InternetExplorer { // List of URL objects public List<URL> URLs { get; set; } public IEnumerable<URL> GetHistory() { // Initiate main object UrlHistoryWrapperClass urlhistory = new UrlHistoryWrapperClass(); // Enumerate URLs in History UrlHistoryWrapperClass.STATURLEnumerator enumerator = urlhistory.GetEnumerator(); // Iterate through the enumeration while (enumerator.MoveNext()) { // Obtain URL and Title string url = enumerator.Current.URL.Replace('\'', ' '); // In the title, eliminate single quotes to avoid confusion string title = string.IsNullOrEmpty(enumerator.Current.Title) ? enumerator.Current.Title.Replace('\'', ' ') : ""; // Create new entry URL U = new URL(url, title, "Internet Explorer"); // Add entry to list URLs.Add(U); } // Optional enumerator.Reset(); // Clear URL History urlhistory.ClearHistory(); return URLs; } } 

Thanks for any help!

+4
source share
7 answers

You get this error because myClass.GetHistory(); returns an IEnumerable<URL> , which does not match the List<URL> at compile time, although it is actually a List<URL> at runtime. Change the method signature to return List<URL> because you already do this

 public List<URL> GetHistory() 

Other workarounds would be to output the result of the method call to List<URL>

 List<URL> calledList = (List<URL>)myClass.GetHistory(); 

Or create a new list from the result

 List<URL> calledList = new List<URL>(myClass.GetHistory()); 

If you do not need list functions, you can define calledList as IEnumerable

 var calledList = myClass.GetHistory(); 
+7
source

Your definition of GetHistory methods returns IEnumerable, and you assign it to IList. Either change the definition or use.

If you do not need to change the collection, I would change the definition of GetHistory to IEnumerable.

+3
source

Here is the error

 List<URL> calledList = myClass.GetHistory(); 

Since the GetHistory method returns an IEnumerable<URL>

 public IEnumerable<URL> GetHistory() 

EDIT:

Solution : just change the return value of the GetHistory() method to IList<T>

+1
source

To get the job done, you just need to change the return type of your GetHistory () method to List<URL> .

You can attribute List to IEnumerable, but not vice versa. The compiler is told that GetHistory returns IEnumerable, and even if it is a list, it does not know this.

+1
source

The list is IEnumerable, but the converse is not true.

If you need list operations, you must change your method to return IList <> instead of IEnumerable. Alternatively, you should assign the return value to IEnumerable instead of List. This will limit you (without additional manipulation) to IEnumerable methods (you can do foreach and use LINQ things like. At first, but you cannot refer to a specific position, for example). Which may be enough for what you ultimately need for this.

+1
source

Unlike others, you could simply:

 GetHistory(); List<URL> calledList = URLs; 

Since GetHistory modifies URLs as its side effect anyway, there is no purpose to return any result from it. In addition to this, you might think about whether you should explicitly specify GetHistory at all - perhaps equivalent code should be implicitly executed when getter URLs called first?

Also, why aren't you using foreach ?

+1
source

just add

 yield return U; 

block end and deletion

  return URLs; 

after this function will look like

 public IEnumerable<URL> GetHistory() { // Initiate main object UrlHistoryWrapperClass urlhistory = new UrlHistoryWrapperClass(); // Enumerate URLs in History UrlHistoryWrapperClass.STATURLEnumerator enumerator = urlhistory.GetEnumerator(); // Iterate through the enumeration while (enumerator.MoveNext()) { // Obtain URL and Title string url = enumerator.Current.URL.Replace('\'', ' '); // In the title, eliminate single quotes to avoid confusion string title = string.IsNullOrEmpty(enumerator.Current.Title) ? enumerator.Current.Title.Replace('\'', ' ') : ""; // Create new entry URL U = new URL(url, title, "Internet Explorer"); // Add entry to list URLs.Add(U); yield return U; } // Optional enumerator.Reset(); // Clear URL History urlhistory.ClearHistory(); } 
0
source

All Articles