Which is better for performance, wise, declaring a variable outside the foreach statment and reassigning it each time (foreach) or creating a new variable inside foreach for example
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; }
or this one?
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; }
Of course I'm talking about the object of an object. thank you all.
scope variable-assignment c #
Ahmed magdy
source share