Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6528291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:31:15+00:00 2026-05-25T09:31:15+00:00

There is a base class which has one method of generic type and I

  • 0

There is a base class which has one method of generic type and I am sure that in my derived I will be returning a string. This is my code:

public abstract class Base
    {
        public virtual T GetSomething<T>()
        {
            return default(T);
        }
    }

    public class Extended : Base
    {
        public override string GetSomething<string>()
        {
            return string.Empty;

            //return base.GetSomething<T>();
        }
    }

But this code doesn’t compile. Can anybody spot the mistake? I am sure that in my Extended class I want to return string only. How do I solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:31:16+00:00Added an answer on May 25, 2026 at 9:31 am

    You cannot override a generic method with a concrete implementation; that’s not how generics work. The Extended class must be able to handle calls to GetSomething<int>() for example.

    In other words, the signature for an overriding method must be identical to the method it is overriding. By specifying a concrete generic implementation of the method, you change its signature.

    Consider using this approach:

    public override T GetSomething<T>()
    {
        if (typeof(T) == typeof(string))
            return string.Empty;
    
        return base.GetSomething<T>();
    }
    

    Note that the JIT should optimize away the conditional when compiling a particular instantiation of this method. (If it doesn’t then it’s not a very good JIT!)

    (The syntax for your override is technically correct, but fails for other reasons as well. For example, you can’t use the keyword string as a generic parameter name. And if you could, your code still wouldn’t do what you want, nor would it compile, since the compiler would be unable to find a method with that signature on a supertype.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an abstract base class Animal which has public virtual abstract method
Let's suppose we have a base class which has a virtual method: class BaseClass
Are there any dictionary classes in the .NET base class library which allow duplicate
Let's say I have a Base class and several Derived classes. Is there any
My discount class has a sales_period. I want to write a method which can
Suppose I have a class Base which has a member variable A* my_hash. I
I have a base class and a dozen derived classes. All but one derived
I have a code base, in which for Matrix class, these two definitions are
Is there anyway to discover the base class of a class in Python? For
Is there a better way of binding a list of base class to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.