tags:

views:

552

answers:

9

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?

+1  A: 

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
+4  A: 

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.

Strelok
+6  A: 

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!
Andrew Kennan
+3  A: 

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
+25  A: 

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.

More information here.

George Stocker
Yep! Just used these myself - It's so awesome not to have to throw around temporary Datasets...
John Dunagan
+4  A: 

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.

gius
+3  A: 

This Article on MSDN should help you immensely.

Perpetualcoder
+1  A: 

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
+2  A: 

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 behaviour 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.

lithander