Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying the type of ArrayList's elements

Tags:

c#

.net

generics

I thought that there was some way in .net 3.0 to give an array list a type so that it didnt just return Object's but I'm having trouble doing so. Is it possible? If so, how?

like image 960
The.Anti.9 Avatar asked Dec 06 '22 07:12

The.Anti.9


1 Answers

List<T> was introduced with generics in .NET 2.0:

using System.Collections.Generic;

var list = new List<int>();
list.Add(1);
list.Add("string"); //compile-time error!
int i = list[0];
like image 57
Mark Cidade Avatar answered Dec 14 '22 22:12

Mark Cidade