Question Locked

Generics

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
Can anyone explain me the exact difference between C# generics and Java generics and C++ templates,I want to know what exactly happens internally while storing this Generic type and How c# generics are more efficient then any of Java generics and c++ templates ????

2 replies

by vermanshul

Starting from the C++:-
In it compiler checks what all different versions of a Template calls can be required. and then generates a copy each for each version. Hence emitting all the possible required code in the binary files.

In Java:-
Java Generics are a step ahead, they internally map each instance as object and hide the type safety from user. But, internally they are used as Object classes.

In C#:-
C# has the latest and till date best implementation of Generics (as far as i know). It would not do anything while compiling, rather than placing some meta tags. Each time it is called for a specific data type, a version of that Generic class/ method is generated and chached (to be used later). So it does not emit all the code before hand .. as in C++; and it also does not convert it to OBJECT as in Java

This discussion is closed to new replies.