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 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

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Something you may want to try to rule out window… May 11, 2026 at 11:34 pm
  • Editorial Team
    Editorial Team added an answer You should be thinking in terms of what makes a… May 11, 2026 at 11:34 pm
  • Editorial Team
    Editorial Team added an answer Probably your windows registered name have a special character like… May 11, 2026 at 11:34 pm

Related Questions

I have a question with fluent interfaces. We have some objects that are used
I have spent some time now to solve a problem for which i have
We have a social site, and want to integrate facebook connect to save time
How can one programmatically fire the SelectedIndexChanged event of a ListView? I've intended for

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.