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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:35:51+00:00 2026-05-21T20:35:51+00:00

I have a lot of unit tests that pretty much tests the same behavior.

  • 0

I have a lot of unit tests that pretty much tests the same behavior. However, data type changes.

I am trying to create a generic method that can take any data type. I tried making my input parameter var but that’s not allowed. Also, looked into c# generics but that usually deals with a list.

  • 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-21T20:35:52+00:00Added an answer on May 21, 2026 at 8:35 pm

    You could make the parameter an object:

    public void DoSomething(object arg)
    {
       //...
    

    Or you could do what I prefer and make a generic method:

    public void DoSomething<T>(T arg)
    {
        //...
    

    The generic approach has two major advantages, and I’ll give examples of why they’re useful:

    1. Even though you don’t explicitly specify the type of arg, you still have access to it.
    2. You can add constraints on the types you want to allow.

    Conversely, the object approach has some important disadvantages:

    1. Since you’re treating arg as an object, you’ll only be able to do things you could do with any object.
    2. If you pass a value type as an object parameter, the variable will be boxed, which means a performance hit. It’s not a huge hit, but if you call DoSomething several thousand times in a row, you might start feeling it.

    Generics and Type Constraints

    Adding a type constraint to a generic method allows you to restrict the method so that it only accepts certain types. Why is that useful? Because even though you don’t know—or care—what specific type you’re working with, you now know something about it, and you can use that information.

    Consider the following setup:

    public interface IAnimal 
    { 
        void Move(); 
    }
    public class Duck : IAnimal
    {
        public void Move() 
        { 
            Console.WriteLine("Flying"); 
        }
    }
    public class Fish : IAnimal
    {
        public void Move()
        { 
            Console.WriteLine("Swimming"); 
        }
    }
    public class Ant : IAnimal
    {
        public void Move()
        { 
            Console.WriteLine("Walking"); 
        }
    }    
    

    Since we have an IAnimal interface, we can write generic methods targeting any implementation of IAnimal:

    public class Program
    {
        static void DoMove<T>(T animal) where T : IAnimal
        {
            animal.Move();
        }
        public static void Main(string[] args)
        {            
            Duck duck = new Duck(); 
            Fish fish = new Fish();
            Ant ant = new Ant(); 
    
            DoMove<Duck>(duck);
            DoMove<Fish>(fish);
            DoMove<Ant>(ant);
        }
    }
    

    Run it: http://rextester.com/GOF1761

    When we write the DoMove method, we don’t care whether its parameter animal is a Duck, a Fish, an Ant, or anything else. All we care about is calling animal.Move(). Since we used the where T : IAnimal constraint, the compiler knows everything we need it to know:

    1. The variable animal is of type T.
    2. Whatever T is, it implements IAnimal.
    3. Anything that implements IAnimal has a Move() method.
    4. Therefore, we can safely call animal.Move().

    (By the way, yes, we could just write DoMove as static void DoMove(IAnimal animal), but that’s another discussion.)

    Type Inference (and some of its implications)

    Fine, but let’s take it a step further. In many cases, you can call generic methods without having to specify their type parameters. This is called type inference, and aside from saving you some typing, it can be useful when doing the same operation on objects of different types.

    public static void Main(string[] args)
    {            
        IAnimal[] animals = new IAnimal[] 
        {
            new Duck(),
            new Fish(),
            new Ant()
        };
    
        foreach (IAnimal animal in animals)
        {
            DoMove(animal);
        }
    }
    

    Run it: http://rextester.com/OVKIA12317

    You only have to write the DoMove<T> method once, and you can call it on any type of IAnimal without having to give a more specific type. The appropriate version of Move will be called each time, because DoMove<T> is able to infer which type to use for T. When you call DoMove(duck), .NET understands that you really mean DoMove<Duck>(duck), which then calls the Move method on the Duck class.

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

Sidebar

Related Questions

We have a lot of unit tests but they aren't run every night. I
I have been creating Unit tests like crazy and find that I'm often having
I have a lot of unit test that depend on a certain configuration of
I have a lot of constants that are somehow related, at some point I
I'm currently trying to get the type that an object was casted as in
I'm trying to do some unit testing on a project that unfortunately has high
We have a lot of open discussions with potential clients, and they ask frequently
we have a lot of users running in different shared and solo-owned repositories in
I have a lot of classes in the App_Code directory could this be a
I have a lot of spare intel linux servers laying around (hundreds) and want

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.