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

The Archive Base Latest Questions

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

I mostly use Java and generics are relatively new. I keep reading that Java

  • 0

I mostly use Java and generics are relatively new. I keep reading that Java made the wrong decision or that .NET has better implementations etc. etc.

So, what are the main differences between C++, C#, Java in generics? Pros/cons of each?

  • 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-10T13:35:24+00:00Added an answer on May 10, 2026 at 1:35 pm

    I’ll add my voice to the noise and take a stab at making things clear:

    C# Generics allow you to declare something like this.

    List<Person> foo = new List<Person>(); 

    and then the compiler will prevent you from putting things that aren’t Person into the list.
    Behind the scenes the C# compiler is just putting List<Person> into the .NET dll file, but at runtime the JIT compiler goes and builds a new set of code, as if you had written a special list class just for containing people – something like ListOfPerson.

    The benefit of this is that it makes it really fast. There’s no casting or any other stuff, and because the dll contains the information that this is a List of Person, other code that looks at it later on using reflection can tell that it contains Person objects (so you get intellisense and so on).

    The downside of this is that old C# 1.0 and 1.1 code (before they added generics) doesn’t understand these new List<something>, so you have to manually convert things back to plain old List to interoperate with them. This is not that big of a problem, because C# 2.0 binary code is not backwards compatible. The only time this will ever happen is if you’re upgrading some old C# 1.0/1.1 code to C# 2.0

    Java Generics allow you to declare something like this.

    ArrayList<Person> foo = new ArrayList<Person>(); 

    On the surface it looks the same, and it sort-of is. The compiler will also prevent you from putting things that aren’t Person into the list.

    The difference is what happens behind the scenes. Unlike C#, Java does not go and build a special ListOfPerson – it just uses the plain old ArrayList which has always been in Java. When you get things out of the array, the usual Person p = (Person)foo.get(1); casting-dance still has to be done. The compiler is saving you the key-presses, but the speed hit/casting is still incurred just like it always was.
    When people mention "Type Erasure" this is what they’re talking about. The compiler inserts the casts for you, and then ‘erases’ the fact that it’s meant to be a list of Person not just Object

    The benefit of this approach is that old code which doesn’t understand generics doesn’t have to care. It’s still dealing with the same old ArrayList as it always has. This is more important in the java world because they wanted to support compiling code using Java 5 with generics, and having it run on old 1.4 or previous JVM’s, which microsoft deliberately decided not to bother with.

    The downside is the speed hit I mentioned previously, and also because there is no ListOfPerson pseudo-class or anything like that going into the .class files, code that looks at it later on (with reflection, or if you pull it out of another collection where it’s been converted into Object or so on) can’t tell in any way that it’s meant to be a list containing only Person and not just any other array list.

    C++ Templates allow you to declare something like this

    std::list<Person>* foo = new std::list<Person>(); 

    It looks like C# and Java generics, and it will do what you think it should do, but behind the scenes different things are happening.

    It has the most in common with C# generics in that it builds special pseudo-classes rather than just throwing the type information away like java does, but it’s a whole different kettle of fish.

    Both C# and Java produce output which is designed for virtual machines. If you write some code which has a Person class in it, in both cases some information about a Person class will go into the .dll or .class file, and the JVM/CLR will do stuff with this.

    C++ produces raw x86 binary code. Everything is not an object, and there’s no underlying virtual machine which needs to know about a Person class. There’s no boxing or unboxing, and functions don’t have to belong to classes, or indeed anything.

    Because of this, the C++ compiler places no restrictions on what you can do with templates – basically any code you could write manually, you can get templates to write for you.
    The most obvious example is adding things:

    In C# and Java, the generics system needs to know what methods are available for a class, and it needs to pass this down to the virtual machine. The only way to tell it this is by either hard-coding the actual class in, or using interfaces. For example:

    string addNames<T>( T first, T second ) { return first.Name() + second.Name(); } 

    That code won’t compile in C# or Java, because it doesn’t know that the type T actually provides a method called Name(). You have to tell it – in C# like this:

    interface IHasName{ string Name(); }; string addNames<T>( T first, T second ) where T : IHasName { .... } 

    And then you have to make sure the things you pass to addNames implement the IHasName interface and so on. The java syntax is different (<T extends IHasName>), but it suffers from the same problems.

    The ‘classic’ case for this problem is trying to write a function which does this

    string addNames<T>( T first, T second ) { return first + second; } 

    You can’t actually write this code because there are no ways to declare an interface with the + method in it. You fail.

    C++ suffers from none of these problems. The compiler doesn’t care about passing types down to any VM’s – if both your objects have a .Name() function, it will compile. If they don’t, it won’t. Simple.

    So, there you have it 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • 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 Using array_keys() and array_values(). $keys = array_keys($array); $values = array_values($array); May 11, 2026 at 11:41 pm
  • Editorial Team
    Editorial Team added an answer No difference as far as I can tell. There is… May 11, 2026 at 11:41 pm
  • Editorial Team
    Editorial Team added an answer This is generally done in the Xcode project properties. Right-click… May 11, 2026 at 11:41 pm

Related Questions

We have bunch of autogenerated classes which are mostly Axis2 stubs, skeletons etc. For
I need to detect whether my application is running within a virtualized OS instance
I'm considering porting a very simple text-templating library to scala, mostly as an exercise
I do mostly Java and C/C++ development, but I'm starting to do more web

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.