2

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

Tarscher
  • 1,923
  • 1
  • 22
  • 45
  • 1
    See [Making a generic property](http://stackoverflow.com/questions/271347/making-a-generic-property). Basically, you can't have a generic property in a non-generic class. – Matthew Flaschen Jul 12 '10 at 08:14

1 Answers1

7

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
  • 115,091
  • 17
  • 196
  • 297
  • 1
    Exactly. Basically, the compiler is reading the line `public IEnumerable Elements` 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 Jul 12 '10 at 08:18