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

  • Home
  • SEARCH
  • 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 88669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:35:46+00:00 2026-05-10T22:35:46+00:00

How can I select the good method (I have in the example below show

  • 0

How can I select the good method (I have in the example below show 2 differents way that doesn’t work). I was using instead of a variable of type Object with a IF and IS to do the job but I am trying to avoid using Object and boxing/unboxing. So I thought that Generic could do the job but I am stuck here.

Here is a small snippet of code that illustrate my question:

class Program {     static void Main(string[] args)     {         Parser p = new Parser();         ObjectType1 o1 = new ObjectType1();         p.execute(o1);         Console.Read();     } }  class Parser {     public T execute<T>(T obj)     {         /*         if (obj is ObjectType1)             this.action((ObjectType1)obj);         else if (obj is ObjectType2)             this.action((ObjectType2)obj);         */         this.action(obj);         return obj;     }      private void action(ObjectType1 objectType1)     {         Console.WriteLine('1');     }      private void action(ObjectType2 objectType2)     {         Console.WriteLine('2');     } }   class ObjectType1 { }  class ObjectType2 { } 

Update

I do not want interface and class. Sorry. I knew that it’s not the goal of the question.

Casting with (ObjectType)obj doesn’t work but if you do :

        if (obj is ObjectType1)             this.action(obj as ObjectType1);         else if (obj is ObjectType2)             this.action(obj as ObjectType1); 

it works… why?

And… I cannot overload for all type the execute method because this method is from an Interface. This is why all need to be called from this method.

  • 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. 2026-05-10T22:35:46+00:00Added an answer on May 10, 2026 at 10:35 pm

    No, you can’t do this. Generics don’t work like C++ templates – the generic method is compiled just once. The only information that the compiler can use for overload resolution is the information it knows about within the generic method, regardless of what code uses it.

    As an example to show this, here’s a bit of code which may not work how you expect it to:

    using System;  class Test {         static void Main()     {         string x = 'hello';         string y = string.Copy(x);          Console.WriteLine(x==y); // Overload used         Compare(x, y);     }      static void Compare<T>(T x, T y) where T : class     {         Console.WriteLine(x == y); // Reference comparison     } } 

    It’s hard to say the best way to proceed without knowing more about what you want to do.

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

Sidebar

Related Questions

I’ve had a good look and can’t seem to find out how to select
Can a JApplet use a JFileChooser so that the user can select a file
I have GridView which I can select a row. I then have a button
Does anyone know of a script that can select all text references to URLs
I have a datagridview where the users can select which subset of columns to
I am building a table using the DataGridView where a user can select items
I can select all the distinct values in a column in the following ways:
I know I can SELECT 5 AS foo and get the resultset: foo 5
On Gnome/KDE you can select in which application you want to open file (Right
I have a question with fluent interfaces. We have some objects that are used

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.