【发布时间】:2019-09-05 06:11:51
【问题描述】:
我对 IEnumerable 和 IEnumerator 进行了研究,关于如何使类可用于 foreach 语句有很多方法。但这并不能回答我的问题。为什么foreach循环中使用的EventLogEntryCollection不返回EventLogEntry?但是,如果我将 var 更改为 EventLogEntry,它会按照我使用 for 循环的方式工作。
EventLog eventLog1 = new EventLog
{
Log = myLogName,
Source = "sourcy"
};
EventLogEntryCollection collection = eventLog1.Entries;
eventLog1.Close();
foreach (var v in collection)
{
Console.WriteLine(v.Message); //object does not contain definition for Message
}
for (int i = 0; i < collection.Count; i++)
{
Console.WriteLine("The Message of the EventLog is :"
+ collection[i].Message);
}
【问题讨论】:
标签: c# collections system.diagnostics