我想创建一个这样的List:
List<int, DateTime> foo = new List<int, DateTime>();
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
Using the generic type 'System.Collections.Generic.List<T>' requires 1 type arguments
是否有可能在C#中做到这一点?
您可以拥有int/DateTime 元组列表.
var foo = new List<Tuple<int, DateTime>>();
Run Code Online (Sandbox Code Playgroud)
这确实需要.Net 4.0+.
我个人更喜欢创建一个简单的类,并将其用于我的列表.我认为它比嵌套泛型更具可读性.
// 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;}
}
Run Code Online (Sandbox Code Playgroud)
人们也可以使用dynamic匿名类型并发送它.
var foo = new List<dynamic>();
foo.Add(new {X = 0, D = DateTime.Now});
foreach(var d in foo)
{
Console.WriteLine(d);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13191 次 |
| 最近记录: |