foreachステートメントの外側で変数を宣言し、そのたびに変数をその側に再割り当てする(foreach)か、foreach内に新しい変数を作成するなど、パフォーマンスの面で優れている点
private List<ListItem> GetItems()
{
var items = new List<ListItem>();
var collection = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ListItem item;
foreach (var i in collection)
{
item = new ListItem { Text = i.ToString() };
items.Add(item);
}
return items;
}
またはこれ?
private List<ListItem> GetItems()
{
var items = new List<ListItem>();
var collection = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
foreach (var i in collection)
{
ListItem item = new ListItem { Text = i.ToString() };
items.Add(item);
}
return items;
}
確かにここで私はアイテムオブジェクトについて話している。皆さん、ありがとうございました。