goul
goul

Reputation: 853

Interface declared as IEnumerable but not usable as such

I have an IEnumerable interface:

public interface IBrands: IEnumerable<Ibrand>

And its instanciation:

public class Brands: IBrands
{
    public Brands()
    {
        _brandsDic= new Dictionary<int, IBrand>();
    }
}

As I used Linq to retreive some data in another class, I had to return some IEnumerable:

public IEnumerable<IBrand> GetCarsBrands()
{
    return _carsDic.SelectMany(r => r.Value.Brands).ToList();
}

... but would like instead to return directly a IBrands. Would you know how? It complains about the casting of the IBrands in a IEnumerable if I try doing so.

Thanks!

Upvotes: 3

Views: 81

Answers (1)

Stefan Steinegger
Stefan Steinegger

Reputation: 64658

The IBrands is an IEnumerable<IBrand>, but not the other way around. So you can't return an IEnumerable<IBrand> as IBrands.

Depending of what Brands actually represents, you could create another instance of Brands.

return new Brands(_carsDic.Values);

Upvotes: 3

Related Questions