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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:26:56+00:00 2026-05-14T06:26:56+00:00

This is an extension of this question and probably might even be a duplicate

  • 0

This is an extension of this questionand probably might even be a duplicate of some other question(If so, please forgive me). I see from MSDN that generics are usually used with collections

The most common use for generic
classes is with collections like
linked lists, hash tables, stacks,
queues, trees and so on where
operations such as adding and removing
items from the collection are
performed in much the same way
regardless of the type of data being
stored.

The examples I have seen also validate the above statement.

Can someone give a valid use of generics in a real-life scenario which does not involve any collections ?

Pedantically, I was thinking about making an example which does not involve collections

public class Animal<T>
{
    public void Speak()
    {
       Console.WriteLine("I am an Animal and my type is " + typeof(T).ToString());
    }

    public void Eat()
    {
        //Eat food
    }
}

public class Dog
{
    public void WhoAmI()
    {
        Console.WriteLine(this.GetType().ToString());

    }
}         

and “An Animal of type Dog” will be

Animal<Dog> magic = new Animal<Dog>();

It is entirely possible to have Dog getting inherited from Animal (Assuming a non-generic version of Animal)Dog:Animal Therefore Dog is an Animal

Another example I was thinking was a BankAccount. It can be BankAccount<Checking>,BankAccount<Savings>. This can very well be Checking:BankAccount and Savings:BankAccount.

Are there any best practices to determine if we should go with generics or with inheritance ?

  • 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-14T06:26:57+00:00Added an answer on May 14, 2026 at 6:26 am

    You’ll probably get some better answers, but meanwhile consider this:
    Generic classes imply an “of” relation between the generic class and the parameter class.

    All dogs are animals, so they share certain attributes/qualities with all other animals. If you use inheritance, then it is very easy to implement those common qualities in the Animal class, and add qualities at decendant classes. But if you implement it using Animal (Of Dog), then:

    1. Your Dog class isn’t really a fully-qualified dog/animal by itself, as its “animal qualities” are contained in the Animal (Of T) class. The Dog must be tied with the Animal in a parent-child relationship.
    2. Your Animal class isn’t really an animal: you cannot create some method which accepts Animals as an argument, because you can’t refer to the globalized Animal (Of T) class. It is actually a family of classes with a common behavior, not a superclass. You lose the benefit of dealing with Animals outside the Animal (Of T) class.

    But here is how, IMHO, you should think of generics:
    Think about a MedicalDoctor(Of T) class.
    Veterinarian(Of T as Animal) would be inheriting from MedicalDoctor(Of Animal).
    DogVeterinarian would inherit from Veterinarian(Of Dog).

    Key point: the generic class and the parameter class are not tightly-coupled and co-dependant, they co-exist.

    BTW, if you want to see some good non-collection usage of generics, just notice the delegates we have: Action(Of T), Func(Of TResult), Comparison(Of T), EventHandler(Of TEventArgs), Converter(Of TSource, TResult)… and the interfaces: IEqualityComparer(Of T), IComparer(Of T)…

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

Sidebar

Related Questions

This is an extension to a question which I asked some time ago. I
This is an extension to the question I asked here: Get text from clipboard
This is an extension of question: Order of the initialization in Java so in
This is an extension of the question I asked here I have a relationship
This is an extension of a question I previously asked here . Long story
This is an extension for this question asked an hour ago. We cannot modify
This is an extension of this question: SQLite problem selecting two columns as one
As an extension to this question here Linking JavaScript Libraries in User Controls I
This is something of an extension to this question: Dispatching to correct function with
This question will probably take a while to explain, and I'll need to provide

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.