Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert type IEnumerable to List

Tags:

c#

I have been using C# with Unity3d for a few years now, but am just starting with .NET programming. I get the error:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<URL>' to 'System.Collections.Generic.List<URL>'. An explicit conversion exists (are you missing a cast?)

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!

like image 677
CC Inc Avatar asked Jun 26 '12 12:06

CC Inc


People also ask

How do I convert IEnumerable to IList?

If you need to go from IEnumerable to IList, you may try a cast first, if you get lucky it will work. If not, you'll have to foreach over the IEnumerable collection and build a new IList. Edit: use ToList() instead of foreaching yourself as others have suggested. Save this answer.

What is IEnumerable interface in C#?

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.


2 Answers

You can just use tolist() provided by Linq lamda to convert from IEnumerable to List.

using System.Linq;

List<URL> calledList = myClass.GetHistory().ToList();
like image 103
Prayan shakya Avatar answered Oct 05 '22 04:10

Prayan shakya


You get that error because myClass.GetHistory(); returns IEnumerable<URL>, which is not same as List<URL> at compile time, although it is actually List<URL> at runtime. Change method signature to return List<URL>, cause you already do that

public List<URL> GetHistory()

Other workarounds would be to cast method call result to List<URL>

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

Or construct new list from result

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

If you do not need List functionality, you could define calledList as IEnumerable

var calledList = myClass.GetHistory();
like image 26
archil Avatar answered Oct 05 '22 05:10

archil