0

I want to create a List like this :

List<int, DateTime> foo = new List<int, DateTime>();

but I get this error :

Using the generic type 'System.Collections.Generic.List<T>' requires 1 type arguments

Is it possible to do that in C# ?

Wassim AZIRAR
  • 10,577
  • 37
  • 118
  • 171

8 Answers8

9

You can have a list of int/DateTime Tuples.

var foo = new List<Tuple<int, DateTime>>();

This does require .Net 4.0+.

I personally prefer creating a simple class and using that for my list. I think it's more readable than nesting generics.

// I don't know your domain so the example is with names I'd hate to actually see
class MyType
{
    public int MyInteger {get; set;}
    public DateTime MyDateTime {get; set;}
}

One could also use dynamic and send it an anonymous type.

var foo = new List<dynamic>();

foo.Add(new {X = 0, D = DateTime.Now});

foreach(var d in foo)
{
    Console.WriteLine(d);
}
Austin Salonen
  • 47,582
  • 15
  • 104
  • 136
4

You can use List<Tuple<int,DateTime>> if you are using .NET 4.0 or above.

An alternative is to create a simple class that will serve as a generic type - the benefit of that is that of readability (giving descriptive names to the type and properties).

List<MyType> myList = new List<MyType>();

class MyType
{
   public int TheInt { get; set; }
   public DateTime TheDateTime { get; set; }
}
Oded
  • 477,625
  • 97
  • 867
  • 998
4

It depends on what you want to do.

You can for example use a Dictionary if you want to use int as an index to access a DateTime like this:

Dictionary<int, DateTime> dict = new Dictionary<int, DateTime>();

dict.Add(1, DateTime.Now);
DateTime d = dict[1];

Or if you want to store an arbitrary list of values and allow duplicates you can use:

  var values = new List<Tuple<int, DateTime>>();
  values.Add(new Tuple<int, DateTime>(1, DateTime.Now));
  Tuple<int, DateTime> value = values.First();
Wouter de Kort
  • 37,915
  • 11
  • 80
  • 100
1

The List<T> type only takes a single generic type argument but you are providing two. If you want to store two values in each slot you need to use a wrapper type which can contain the two values. For example

class Storage {
  public int IntValue { get; set; }
  public DateTime DateValue { get; set; }
}

List<Storage> list = ...;

If you would like to avoid creating a custom type you can also use Tuple<int, DateTime>

JaredPar
  • 703,665
  • 143
  • 1,211
  • 1,438
0

You could achieve this using a Tuple<int, DateTime> or KeyValuePair<int, DateTime>, collected within a List<T>. You get the error because the List<T> contains one generic parameter as it stores a single collection of elements (which are all of one base type).

Samuel Slade
  • 8,107
  • 5
  • 31
  • 54
0

Why not create a class that encapsulate the two properties ?

public class Foo
{
 public int ID { set;get;}
 public DateTime CreatedDate { set;get;}

}

Then you can create a List of that class

 List<Foo> objFooList=new List<Foo>();
Shyju
  • 206,216
  • 101
  • 402
  • 492
0

It's List<T> with one generic type parameter, not List<T, K>. Maybe you want to use a Dictionary<TKey, TValue> instead?

Kris Krause
  • 7,228
  • 2
  • 21
  • 26
0

List can only contain one type. You can do two things here:

  1. Create a list of tuple

    List<Tuple<int,DateTime>>

  2. Use dictionary

    Dictionary<int, DateTime>

Emmanuel N
  • 7,202
  • 2
  • 25
  • 36