Questions tagged [generics]

Generics are a form of parametric polymorphism found in a range of languages, including .NET languages, Java, Swift, Rust and Go (since 1.18).

is a language feature found in certain languages to enable a form of . They typically allow the programmer to express concepts such as "A list of some type T" in a type-safe manner. Prior to the addition of generics to the language and the , programmers using these languages were forced to downcast from the base Object when using some general purpose classes, such as collection classes.

With the addition of , the programmer can instead use types such as List<int> to create type-safe lists which only store int objects.

In-depth detail for examples and concepts specifically for C# Generics is provided by Microsoft here. Information on Java generics can be found here.

Unlike , generics are typically limited to simple type substitution, without the ability of templates to specialize in specific types (infamously misused in the C++ standard library in std::vector<bool> which behaves radically different from any other std::vector<T>). This also means that generics are not well suited for , which typically relies on an ability to tailor generic algorithms for specific parameter types (again using a C++ example, pointers are usable with any generic algorithm expecting arguments to be iterators).

Java Generics Tutorials

  1. Java Generic methods and generic classes Tutorials
  2. Java Generics FAQs

.NET Generics Tutorials

  1. Introduction to Generics
  2. C# Generics

Rust Generics Tutorials

  1. Generics chapter from The Rust Book
  2. Generics section from Rust By Example

Go Generics Tutorials

  1. Tutorial: Getting started with generics

Example

C# without Generics

var list = new System.Collections.ArrayList();
list.Add(1);
list.Add("banana"); // will compile

int n = (int) list[0];
int s = (int) list[1]; // will compile, but throws an InvalidCastException

C# with Generics

var list = new System.Collections.Generic.List<int>();
list.Add(1);
//list.Add("banana"); -- Will not compile

int n = list[0];
//string s = list[1]; -- will not compile
49056 questions
328
votes
4 answers

Generic method with multiple constraints

I have a generic method which has two generic parameters. I tried to compile the code below but it doesn't work. Is it a .NET limitation? Is it possible to have multiple constraints for different parameter? public TResponse Call
Martin
  • 39,309
  • 62
  • 192
  • 278
325
votes
6 answers

What is the difference between 'E', 'T', and '?' for Java generics?

I come across Java code like this: public interface Foo {} public interface Bar {} public interface Zar {} What is the difference among all three of the above and what do they call this type of class or interface declarations in Java?
ace
  • 11,526
  • 39
  • 113
  • 193
319
votes
16 answers

What's the reason I can't create generic array types in Java?

What's the reason why Java doesn't allow us to do private T[] elements = new T[initialCapacity]; I could understand .NET didn't allow us to do that, as in .NET you have value types that at run-time can have different sizes, but in Java all kinds of…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
318
votes
12 answers

Create instance of generic type whose constructor requires a parameter?

If BaseFruit has a constructor that accepts an int weight, can I instantiate a piece of fruit in a generic method like this? public void AddFruit()where T: BaseFruit{ BaseFruit fruit = new T(weight); /*new Apple(150);*/ …
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
312
votes
15 answers

Get generic type of java.util.List

I have; List stringList = new ArrayList(); List integerList = new ArrayList(); Is there a (easy) way to retrieve the generic type of the list?
Thizzer
  • 16,153
  • 28
  • 98
  • 139
310
votes
28 answers

Convert generic List/Enumerable to DataTable?

I have few methods that returns different Generic Lists. Exists in .net any class static method or whatever to convert any list into a datatable? The only thing that i can imagine is use Reflection to do this. IF i have this: List
Josema
  • 4,638
  • 6
  • 22
  • 15
308
votes
20 answers

How do you cast a List of supertypes to a List of subtypes?

For example, lets say you have two classes: public class TestA {} public class TestB extends TestA{} I have a method that returns a List and I would like to cast all the objects in that list to TestB so that I end up with a List.
Jeremy Logan
  • 47,151
  • 38
  • 123
  • 143
307
votes
7 answers

What's the difference between SortedList and SortedDictionary?

Is there any real practical difference between a SortedList and a SortedDictionary? Are there any circumstances where you would specifically use one and not the other?
Shaul Behr
  • 36,951
  • 69
  • 249
  • 387
305
votes
5 answers

What is a higher kinded type in Scala?

You can find the following on the web: Higher kinded type == type constructor? class AClass[T]{...} // For example, class List[T] Some say this is a higher kinded type, because it abstracts over types which would be compliant with the…
Lutz
  • 4,675
  • 4
  • 18
  • 23
299
votes
11 answers

How to use Class in Java?

There's a good discussion of Generics and what they really do behind the scenes over at this question, so we all know that Vector is a vector of integer arrays, and HashTable is a table of whose keys are strings and values…
Karl
  • 8,967
  • 5
  • 29
  • 31
291
votes
5 answers

How to Pass Parameters to Activator.CreateInstance()

I want to create an instance of a type that I specify in a generic method that I have. This type has a number of overloaded constructors. I'd like to be able to pass arguments to the constructors, but Activator.CreateInstance() doesn't see to…
DaveDev
  • 41,155
  • 72
  • 223
  • 385
288
votes
5 answers

Why don't Java Generics support primitive types?

Why do generics in Java work with classes but not with primitive types? For example, this works fine: List foo = new ArrayList(); but this is not allowed: List bar = new ArrayList();
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
285
votes
7 answers

Java generics type erasure: when and what happens?

I read about Java's type erasure on Oracle's website. When does type erasure occur? At compile time or runtime? When the class is loaded? When the class is instantiated? A lot of sites (including the official tutorial mentioned above) say type…
afryingpan