tags:

views:

53

answers:

1

Hi all,

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

    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

+6  A: 

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.

leppie
Exactly. Basically, the compiler is reading the line `public IEnumerable<T> Elements<T>` and thinking that because there's a generic type parameter, it must be a method, and then looks for a `(`, which it doesn't find of course, because you want it to be a property.
Noldorin