'Lexical' scoping of type parameters in C#

Posted by leppie on Stack Overflow See other posts from Stack Overflow or by leppie
Published on 2010-05-13T08:32:37Z Indexed on 2010/05/13 8:34 UTC
Read the original article Hit count: 272

Filed under:
|
|
|

I have 2 scenarios.

This fails:

class F<X>
{
  public X X { get; set; }
}

error CS0102: The type 'F' already contains a definition for 'X'

This works:

class F<X>
{
  class G
  {
    public X X { get; set; }
  }
}

The only logical explanation is that in the second snippet the type parameter X is out of scope, which is not true...

Why should a type parameter affect my definitions in a type?

IMO, for consistency, either both should work or neither should work.

Any other ideas?

PS: I call it 'lexical', but it probably is not not the correct term.

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics