在下面的代碼中,如何將myLine聲明爲公共(全局)變量?問題是我不能使用關鍵字「var」。如何使用匿名類型(C#)聲明字段
public static IEnumerable<string> ReadLines(StreamReader reader)
{
while (!reader.EndOfStream)
{
yield return reader.ReadLine();
}
}
private void Filter1(string filename)
{
using(var writer = File.CreateText(Application.StartupPath + "\\temp\\test.txt"))
{
using (var reader = File.OpenText(filename))
{
int[] Ids = { 14652, 14653, 14654, 14655, 14656, 14657, 14658, 14659, 14660 };
var myLine = from line in ReadLines(reader)
where line.Length > 1
let id = int.Parse(line.Split('\t')[1])
where Ids.Contains(id)
let m = Regex.Match(line, @"^\d+\t(\d+)\t.+?\t(item\\[^\t]+\.ddj)")
where m.Success == true
select new { Text = line, ItemId = id, Path = m.Groups[2].Value };
foreach (var id in myLine)
{
writer.WriteLine("Item Id = " + id.ItemId);
writer.WriteLine("Path = " + id.Path);
writer.WriteLine("\n");
}
}
}
}
我想這樣做,因爲我必須找到一種方法來獲得對該閏日的訪問權限以備後用。
http://blogs.msdn.com/ericlippert/archive/2009/01/26/why-no-var-on-fields.aspx – 2009-06-08 16:22:25