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

104
Views
Abstract generic overlaps with non generic of base class

I have the following base classes

The non-generic:

public abstract class Foo
{
    public abstract bool DoMagic(string str1, string str2);
}

The generic class, inheriting from the non-generic:

public abstract class Foo<T> : Foo
{
    public abstract bool DoMagic(T t1, T t2);
}

Now I want to implement this class

public class FooNumeric : Foo<int>
{
    public override bool DoMagic(string str1, string str2) => true;
    public override bool DoMagic(int int1, int int2) => true;
}

Everything works as excepted as you can see here:

https://dotnetfiddle.net/kWQUl9

Now I would like to not use int as the generic constraint but instead string.

public class FooString : Foo<string>
{
    public override bool DoMagic(string str1, string str2) => true;
    //public override bool DoMagic(string int1, string int2) => true;
}

But I can't seem to satisfy the compiler and I keep getting the error

'FooString' does not implement inherited abstract member 'Foo.DoMagic(string, string)'

https://dotnetfiddle.net/rfncTE

I have tried a couple of things, but to no success.

Is this possible?

Solutions I know that could fix this

  1. Use the non-generic class on FooString

    Can't do this because Foo<T> contains many more generic methods and I have this overlapping on only one occasion.

  2. Rename the method

    if possible I would like to stick to the names.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

In my opinion, the method that receives generic type parameters (DoMagic(T, T)) should have a more general name than a corresponding method that receives concrete types (e.g. DoMagic(string, string)). It is not just that you have a syntactic collision, but you also have a semantical collision in a sense that general and particular concepts share the same name.

Here is one naming scheme where a more concrete method (receiving string) has a more specific name, telling that it will do stuff with the strings.

public class Foo<T>
{
    public bool DoMagic(T a, T b) { ... }
}

public class FooString : Foo<string>
{
    public bool DoMagicWith(string a, string b) { ... }
}

Another naming scheme is to note that general method (receiving the generic type parameter) is performing an operation with a wider definition than any method with concrete parameter types:

public class Foo<T>
{
    public bool DoAnyMagic(T a, T b) { ... }
}

public class FooString : Foo<string>
{
    public bool DoMagic(string a, string b) { ... }
}
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!