27

I'm trying to build my first generic list and have run into some problems. I understand the declaration looks like " List<T> ", and I have using System.Collections.Generic; at the top of my page. However, Visual Studio doesn't recognize the T variable.

What am I missing?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Agile Noob
  • 2,305
  • 4
  • 24
  • 35
  • 2
    Don't feel bad, I went through the exact same thing when I started learning .NET 2.0. I kept wondering, "What the hell does T mean, and why doesn't Visual Studio know what I'm trying to do?" – George Stocker Dec 11 '08 at 23:22
  • So if I want to have different types of objects in my list I need to use an interface, ie List, so store catObj, and dogObj, for example? – Agile Noob Dec 11 '08 at 23:23
  • Yes, List would be a good example of how one can leverage generics. – Kon Dec 12 '08 at 17:00
  • [This Article][1] on MSDN should help you immensely. [1]:http://msdn.microsoft.com/en-us/library/512aeb7t.aspx – Perpetualcoder Dec 11 '08 at 23:25

8 Answers8

47

List<T> means List<WhateverTypeYouWantItToBeAListOf>. So for example:

If I have an Employee Class, and I wanted a collection of Employees, I could say:

List<Employee> employeeList = new List<Employee>();

I could then add Employee Objects to that list, and have it be Type-safe and extend to however many employee objects I put in it.

Like so:

Employee emp1 = new Employee();
Employee emp2 = new Employee();

employeeList.Add(emp1);
employeeList.Add(emp2);

employeeList now holds emp1 and emp2 as objects.

There are several facets to generic collections, the most important being they provide an object independent way of having a... well... collection of objects. They are type-safe; which means that any collection will consist of one type of object. You won't have a Animal instance inside of List<Employee> (unless, of course, Employee is a base class that Animal inherits from. At that point, however, you have bigger problems.

Programming with Generics is its own topic, worthy of (at least) one chapter in a book. At a very high level, programming with generics provides another way to reuse code -- independent of any class hierarchy or implementation details of a specific class.

More information here.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
11

You need to substitute T for another Type such as int, string or a class of your creation:

List<int> values = new List<int>();

values.Add(1);
values.Add(2);

int secondNumber = values[1]; // Yay! No casting or boxing required!
George Stocker
  • 57,289
  • 29
  • 176
  • 237
Andrew Kennan
  • 13,947
  • 3
  • 24
  • 33
7

For example if you wanted a generic list of integers (int) you would write:

List<int> list = new List<int>();

T is a type parameter, that in your code should be an actual type.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Strelok
  • 50,229
  • 9
  • 102
  • 115
6

What makes a "generic list" generic is the fact, that you can use the same class to create a list of apples and a list of potatoes. Of course once you create a list with the new operator you have to decide what objects you want it to store and communicate your decision by putting the Type name of the objects you store in the <>.

The following line actually tells the compiler:

List<Employee> employeeList = new List<Employee>();

"Hey, I need a list that can handle objects of type Employee; and is named employeeList!"

The list object you get that way is not generic anymore. It will only accept and return objects of type Employee. But the list's behavior is defined in a "generic" way that doesn't care about what objects it's actually working with. It just calls them 'T' internally because for all that the list is supposed to do, it's contents type doesn't really matter.

Final Form
  • 318
  • 5
  • 12
lithander
  • 149
  • 1
  • 5
5

Consider T as a variable name. You must specify its value, ie. make the generic class 'specific'. So, you can create List<string>, List<int>, or List of anything else you want.

miguel
  • 73
  • 1
  • 2
  • 7
gius
  • 9,289
  • 3
  • 33
  • 62
4

You declare a List of Foo objects like so:

List<Foo> fooList;

The in the type name is a placeholder for the 'real' type of object you put in to the generic list at the time you instantiate it.

Harper Shelby
  • 16,475
  • 2
  • 44
  • 51
4

When you do a List<Object>, make sure your Object class is public. In default VS installations, classes come out of the box as 'internal' - meaning you can't make a list of them because they're less accessible than the public List you're declaring.

This got me um... yesterday. Stupid defaults.

John Dunagan
  • 1,445
  • 3
  • 18
  • 30
2

Are you trying to use the List class or are you trying to build your own? What does your code look like at the moment?

Aistina
  • 12,435
  • 13
  • 69
  • 89