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# ?
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# ?
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);
}
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; }
}
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();
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>
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).
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>();
It's List<T>
with one generic type parameter, not List<T, K>
. Maybe you want to use a Dictionary<TKey, TValue>
instead?
List can only contain one type. You can do two things here:
Create a list of tuple
List<Tuple<int,DateTime>>
Use dictionary
Dictionary<int, DateTime>