Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
The generic type already contains a definition

If I try to define the following Pair<A, B> class in C#, I get a compiler error.

public class Pair<A, B>
{
    public Pair(A a, B b)
    {
        this.A = a;
        this.B = b;
    }

    public A A { get; }
    public B B { get; }
}

The compiler error is:

error CS0102: The type 'Pair<A, B>' already contains a definition for 'A'
error CS0102: The type 'Pair<A, B>' already contains a definition for 'B'

Where are the conflicting definitions?

Usually, one can define a property with the same name as its type, e.g.:

public Guid Guid { get; }
public Uri Uri { get; }

Why is the compiler complaining about the names A and B?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It's specified in the "Class members" section in the C# standard. In the current draft-v6 branch:

The name of a type parameter in the type_parameter_list of a class declaration shall differ from the names of all other type parameters in the same type_parameter_list and shall differ from the name of the class and the names of all members of the class.

In other words, you can't get a type parameter the same name as another type parameter or a class member. Here, you have a type parameter called A and a property called A.

The fact that the type of the property is also A is irrelevant; this code gives the same error:

class Broken<T>
{
    public string T { get; set; }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!