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 8007089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:42:59+00:00 2026-06-04T17:42:59+00:00

I am working with a code that contains following overloaded method in generic class:

  • 0

I am working with a code that contains following overloaded method in generic class:

public class A<T>
{
    public void Process(T item) { /*impl*/ }
    public void Process(string item) { /*impl*/ }
}

When parametrizing the class for string do I lose the possibility to call the version with generic parameter?

var a = new A<string>();
a.Process(""); //Always calls the non-generic Process(string)
  • 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-06-04T17:43:01+00:00Added an answer on June 4, 2026 at 5:43 pm

    There is one way I just discovered, but it’s a bit cross-eyed. Because generics and overloading get resolved in build time, you can define a generic method:

    public static CallerClass
    {
        public static CallGenericOverload<T>(GenericClass<T> cls, T val)
        {
            return cls.ProblemOverload(val); 
        }   
    
        //We can also make an extension method. 
        //We don't have to of course, it's just more comfortable this way.
        public static CallGenericOverloadExtension<T>(this GenericClass<T> cls, T val)
        {
            return cls.ProblemOverload(val);
        }
    
    }
    
    public GenericClass<T>
    {
         public string ProblemOverload(T val)
         {
             return "ProblemOverload(T val)";
         }
         public string ProblemOverload(string val)
         {
             return "ProblemOverload(string val)";
         }
    }
    

    Now, if we do the following:

    var genClass = new GenericClass<string>();
    Console.WriteLine(genClass.ProblemOverload("")); //output: ProblemOverload(string val)
    Console.WriteLine(CallerClass.CallGenericOverload(genClass, "")); //output: ProblemOverload(T val)
    Console.WriteLine(genClass.CallGenericOverloadExtension("")); //output: ProblemOverload(T val)
    

    You can use a similar trick if you define a generic class instead of a generic method. The important thing is that the parameter you transfer to ProblemOverload needs to be of type T rather than type string in the invocation. After all, the method CallGenericOverload knows it’s getting a T at build time, so it’s going to bind to the overload that accepts the parameter. It doesn’t matter that it’s actually going to get a string at runtime.

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

Sidebar

Related Questions

I'm working with the following code that contains a JProgressBar inside of an AbstractTableModel.
I've recently been working with code that looks like this: using namespace std; class
I have a very small header that contains the following code. The code compiles
A Rails 3 application I'm working on contains some logic that invokes the following
I'm working with the following code that is contained in a .ASPX.CS page... img
The following code prints something like °Ð½Ð´Ð¸Ñ-ÐÑÐ¿Ð°Ð½Ð¸Ñ getDirectoryContents path/to/directory/that/contains/files/with/nonASCII/names >>= mapM_ putStrLn Looks like
I'm working on a GUI in SDL. I've created a slave/master class that contains
I had a working code that gave me the address of a mesh (if
Can someone please post the simplest of working code that can get the last
I this working Ruby code that i want to make an executable from with

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.