Reputation: 595
I'm brand new to c# and have read threads on the topic, but am still having trouble. Thanks in advance to your suggestions and advice.
From the top level, I'm implementing an interface that includes a method
IEnumerable<Iinterfacetype> methodname(inputtype input)
In this case, it seems that I should return an object of type IEnumerable<Iinterfacetype>
. In order to do so, I am trying to create a new script that implements IEnumerable<Iinterfacetype>
. My script so far, look like this:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Project3.SimulatedTools.DBImplementations
{
class Enumerable : IEnumerable<interfacetype>
{
List<interfacetype> mylist = new List<interfacetype>();
public IEnumerator<interfacetype> GetEnumerator()
{
return mylist.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
}
Where I have another script called interfacetype.cs which implements Iinterfacetype.
At the original top level, I've set it up like so:
public IEnumerable<Iinterfacetype> methodname(inputtype input)
{
Enumberable<interfacetype> tempnamehere = new Enumerable();
return(tempnamehere);
}
However, I keep getting the error:
Cannot implicitly convert type '
Project3.SimulatedTools.DBImplementations.Enumerable
' toSystem.Collections.Generic.IEnumerable<Iinterfacetype>
'. An explicit conversion exists (are you missing a cast?)
How do I interpret this error? Did I set up the implementation of IEnumerable<interfacetype>
correctly? Why can't I return type IEnumerable<interfacetype>
when interfacetype implements Iinterfacetype?
EDIT: My top level now looks like this, and I've adjusted my class Enumerable to implement IEnumerable instead of , changing all references to to . I no longer get any compile errors, but I'm still not understanding what the original issue is, and haven't set it up to run yet.
public IEnumerable<Iinterfacetype> methodname(inputtype input)
{
Enumberable tempnamehere = new Enumerable();
return(tempnamehere);
}
Furthermore, is it necessary for me to have an interfacetype : Iinterfacetype, as I've set it up now? It seems like I never actually use interfacetype, in which case, it would be irrelevant. If so, why is this the case (that I don't have to implement it)?
Upvotes: 1
Views: 162
Reputation: 149636
Lets look at the definiton of IEnumerable<T>
:
Exposes the enumerator, which supports a simple iteration over a collection of a specified type
Basically, any type implementing IEnumerable
enables us to iterate over a given collection.
There are types in the .NET Framework which implement the interface:
List<T>
, Dictionary<TKey, TValue>
, etc.
Instead of implementing the IEnumerable<T>
yourself, you can use the types supplied by the BCL (Base Class Library) and simply use an object which implements it.
In your case, a List<interfacetype>
would suffice:
public IEnumerable<Iinterfacetype> methodname(inputtype input)
{
return new List<interfacetype>();
}
Upvotes: 2
Reputation: 12459
You really never create a "new Enumerable", instead you create things that impliment IEnumerable. The most obvious example would be:
var l = new List<string>();
This implements IEnumerable<string>;
Upvotes: 1