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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:28:40+00:00 2026-05-29T05:28:40+00:00

I have a method which uses a generic parameter and requires the downcasted form

  • 0

I have a method which uses a generic parameter and requires the downcasted form of the instance. For example,

public abstract class Animal {
}
public class Dog : Animal {
}
public class Cat : Animal {
}
public class AnimalHandler {
    public virtual void Pet<T>(T animal)
    {
    }
}

Given a collection of animals.

public List<Animal> Animals { get; set; }

How do I downcast individual animals before calling the Pet method?

I currently having working in this form.

if (animal is Dog) {
    _animalHandler.Pet((Dog)animal);
}
if (animal is Cat) {
    _animalHandler.Pet((Cat)animal);
}

Ideally, it would be something of this form.

var type = animal.GetType();
_animalHandler.Pet(animal.CastTo(type));

Clarification: I need the instance to be of the downcasted type before it is passed to the method. I need a Dog or Cat specifically to be passed in.

  • 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-29T05:28:41+00:00Added an answer on May 29, 2026 at 5:28 am

    You can accomplish this using the new .NET 4 dynamic keyword:

    namespace Rextester
    {
        public class Program
        {
        public abstract class Animal {
        }
        public class Dog : Animal {
        }
        public class Cat : Animal {
        }
        public class AnimalHandler {
            public virtual void Pet<T>(T animal) {
                Console.WriteLine(typeof(T));
            }
        }
    
        public class Problem {
            public List<Animal> Animals { get; set; }
            private readonly AnimalHandler _animalHandler;
    
            public Problem(AnimalHandler animalHandler) {
                _animalHandler = animalHandler;
                Animals = new List<Animal> { new Cat(), new Dog() };
            }
    
            public void MakeDecision() {
                foreach (var animal in Animals) {
                     _animalHandler.Pet(animal);
                }
            }
        }
    
            public static void Main(string[] args)
            {
                List<Animal> animals = new List<Animal>();
                animals.Add(new Dog());
                animals.Add(new Cat());
    
                var handler = new AnimalHandler();
                handler.Pet((dynamic)animals[0]);
                handler.Pet((dynamic)animals[1]);
            }
        }
    }
    

    Notice the cast to dynamic in the calls to Pet. Though doing it this way negates the necessity of Pet being generic in the first place.

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

Sidebar

Related Questions

I have a container class that has a generic parameter which is constrained to
I have a main page which uses a ViewModel I have created: public class
I have a public method which uses a variable (only in the scope of
I have a method, which uses random samples to approximate a calculation. This method
I have a method which is given the parameter bool sortAscending. Now I want
I have a method that uses a list which I need to pass into
I'm programming in C++ and have a method which uses a static variable. The
I have a WCF service, in which one of the method(or operation contract) uses
I have a method which takes params object[] such as: void Foo(params object[] items)
I have a method which should be executed in an exclusive fashion. Basically, it's

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.