Reputation: 35213
public IEnumerable<decimal> SomeKeys
{
get
{
return dbContext.SomeTable.Select(x=>x.Key);
}
}
public IEnumerable<decimal> SomeOtherKeys
{
get
{
var ret = IEnumerable<decimal>(); // interface name is not
// valid as this point
// do stuff with ret
return ret;
}
}
With my current code, I'm getting the exception above.
Do I have to return a List<decimal>
? Or how am I supposed to return the IEnumerable
or IQueriable
datatypes?
Upvotes: 3
Views: 12668
Reputation: 68440
You create class instances but never interfaces as they are just code contracts but can't be instantiated.
You have to pick the collection implementing IEnumerable<T>
that suit better for your scenario and stick with it.
Your property SomeOtherKeys
can keep the signature, no need to change it as using an interface as return value is totally valid and a good practice as helps to reduce coupling
Upvotes: 1
Reputation: 27349
In your SomeOtherKeys
property getter, you have to instantiate a class that implements the IEnumerable<decimal>
interface.
Changing to List<decimal>
would be fine.
var ret = new List<decimal>();
ret.Add(1.0M);
ret.Add(2.0M);
Upvotes: 1
Reputation: 62276
This is var ret = IEnumerable<decimal>();
just not valid C#
code, that is.
You may want to do something like:
var ret = new List<decimal>();
Remeber that List, quoting documentation, derives from IEnumerable<T>
too.
public class List<T> : IList<T>, ICollection<T>,
IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>,
IEnumerable
so the code like
public IEnumerable<decimal> SomeOtherKeys
{
get
{
var ret = new List<decimal>();
// do stuff with ret
return ret;
}
}
is perfectly valid.
Upvotes: 11