convert a method into a getter
I have a generic method (in a non generic class) returning elements.
public IEnumerable<T> GetElements<T>() where T : class
{
foreach (Element element in elements)
{
if (element is T)
{
yield return element as T;
}
}
}
I want to transform this function in a getter method and tried something like
开发者_StackOverflow中文版public IEnumerable<T> Elements<T>
{
get
{
foreach (Element element in elements)
{
if (element is T)
{
yield return element as T;
}
}
}
}
This does not compile: ( expected
Some one knows what the problem is here?
thanks
Properties do not support generic parameters.
The only way to achieve something like this would be to supply a generic type parameter to the encapsulating type.
精彩评论