C# 添加列表<;日期时间>;列表中的值<;T>;
这可能有点棘手。基本上我有一个这样的类:C# 添加列表<;日期时间>;列表中的值<;T>;,c#,list,datetime,C#,List,Datetime,这可能有点棘手。基本上我有一个这样的类: class Timer { public string boss { get; set; } public List<DateTime> spawnTimes { get; set; } public TimeSpan Runtime { get; set; } public BossPriority priority { get; set; } } List<Timer> bosses = n
class Timer
{
public string boss { get; set; }
public List<DateTime> spawnTimes { get; set; }
public TimeSpan Runtime { get; set; }
public BossPriority priority { get; set; }
}
List<Timer> bosses = new List<Timer>();
不幸的是,这给了我一个“对象引用未设置为对象的实例。”错误
这样做也没什么区别:(
Timer boss=new Timer();
DateTime t1=DateTime.ParseExact(“07:00+0000”,“hh:mm zzz”,CultureInfo.InvariantCulture);
DateTime t2=DateTime.ParseExact(“11:30+0000”,“hh:mm zzz”,CultureInfo.InvariantCulture);
boss.spawhtimes.AddRange(新列表{t1,t2});
我真的要在每个DateTime上添加Do.Add()吗?您已经接近了……您只是忘记声明一个新实例 添加
new[]
,然后将数组强制转换为列表
:
bosses.Add(new Timer { boss = "Tequatl", priority = BossPriority.HardCore,
spawnTimes = new[] { DateTime.ParseExact("07:00 +0000", "hh:mm zzz", CultureInfo.InvariantCulture) }.ToList() });
在使用它之前,必须初始化spawnTimes集合
boss.spawnTimes = new List<DateTime>();
boss.spawhtimes=新列表();
试试这个:
spanTimes = new List<DateTime>{ DateTime.ParseExact("07:00 +0000", "hh:mm zzz", CultureInfo.InvariantCulture) }
spanTimes=新列表{DateTime.ParseExact(“07:00+0000”,“hh:mm zzz”,CultureInfo.InvariantCulture)}
基本上,使用列表初始值设定项语法用您想要的值初始化一个新列表。您的NRE是由于您没有初始化
计时器。spawhtimes
造成的
如果将类初始化为默认构造函数的一部分,则可以在键入时保存:
public class Timer {
public List<DateTime> SpawnTimes { get; private set; }
...
public Timer() {
this.SpawnTimes = new List<DateTime>();
}
}
这在实践中是这样使用的:
bosses.Add( new Timer("Tequat1", BossPriority.HardCore, "07:00 +0000" ) );
bosses.Add( new Timer("Tequat2", BossPriority.Nightmare, "01:00 +0000", "01:30 +0000" ) );
bosses.Add( new Timer("Tequat3", BossPriority.UltraViolence, "12:00 +0000" ) );
还有:FxCop/StyleCop时间!
- 类型(如类)应为
PascalCase
- 公共成员也应该是
(不像Java中的PascalCase
)camelCase
- e、 g.
应为public bossprority priority
public bossprority priority
- e、 g.
- 集合成员不应通过可变属性公开(即使用
而不是private set
(隐含公共)set
- 公共集合成员应该是
或collection
而不是ReadOnlyCollection
或List
T[]
boss.Add(新定时器{boss=“Tequatl”,priority=bossprority.HardCore,spanTimes=newlist{DateTime.ParseExact(“07:00+0000”,“hh:mm zzz”,CultureInfo.InvariantCulture)});
您必须
new
您的spawhtimes
您已接近,但需要包含列表实例化。请重试
bosses.Add(new Timer { boss = "Tequatl", priority = BossPriority.HardCore, spanTimes = new List<DateTime>{ DateTime.ParseExact("07:00 +0000", "hh:mm zzz", CultureInfo.InvariantCulture) } });
boss.Add(新定时器{boss=“Tequatl”,priority=bossprority.HardCore,spanTimes=newlist{DateTime.ParseExact(“07:00+0000”,“hh:mm zzz”,CultureInfo.InvariantCulture)});
如果要这样做,最好只使用新列表{DateTime.ParseExact(..)}
而不是创建一个数组,然后抛出它在上面创建一个列表。
。编辑:另外,这不是“强制转换”数组被复制到一个列表中;它正在创建一个新的列表,其中包含复制的元素。是的,我刚刚也签出了它,它也工作得很好^^^。我将不得不标记该列表,抱歉:)这绝对是我的首选选项。这有一个额外的好处,即如果在未显式构造/分配列表对象的情况下构造计时器,则以后不必处理null
引用。也就是说,var t=new Timer();t.spawhtimes.Add(…)
是可以的。此外,您可以放心地迭代任何计时器
实例,而不必先执行null
检查。最后,它使您的代码更简单,因为您不必在实例化计时器的每个地方重复列表
构造@grantwiney:No,thespawnTimes={DateTime….}
语法将为构建期间分配的列表上的每个项调用Add方法。这不会覆盖SpawnTimes
的实例。编辑:仅当您有SpawnTimes=新列表{DateTime.Parse…}时,它才会覆盖
@grantwiney:是的,文档中有点隐藏了它。在“26.4.2集合初始值设定项”一节中有一个例子。我认为行为的相关声明在同一页的第26.4节:一个成员初始值设定项,在等号后指定集合初始值设定项是嵌入式集合的初始化。不是将新集合分配给字段或属性,而是将初始值设定项中给出的元素添加到集合中字段或属性引用的操作。
public class Timer {
public List<DateTime> SpawnTimes { get; private set; }
...
public Timer() {
this.SpawnTimes = new List<DateTime>();
}
public Timer(String boss, /*String runtime,*/ BossPriority priority, params String[] spawnTimes) : this() {
this.Boss = boss;
// this.Runtime = TimeSpan.Parse( runtime );
this.Priority = priority;
foreach(String time in spawnTimes) {
this.SpawnTimes.Add( DateTime.ParseExact( time, "HH:mm" ) );
}
}
}
bosses.Add( new Timer("Tequat1", BossPriority.HardCore, "07:00 +0000" ) );
bosses.Add( new Timer("Tequat2", BossPriority.Nightmare, "01:00 +0000", "01:30 +0000" ) );
bosses.Add( new Timer("Tequat3", BossPriority.UltraViolence, "12:00 +0000" ) );
bosses.Add(new Timer { boss = "Tequatl", priority = BossPriority.HardCore, spanTimes = new List<DateTime> { DateTime.ParseExact("07:00 +0000", "hh:mm zzz", CultureInfo.InvariantCulture) } });
bosses.Add(new Timer { boss = "Tequatl", priority = BossPriority.HardCore, spanTimes = new List<DateTime>{ DateTime.ParseExact("07:00 +0000", "hh:mm zzz", CultureInfo.InvariantCulture) } });