-2

Follow up question from How to select List<> by its index and return the content?

Can someone please comment if the elements in the list are expected to retain order? In the example above, is "Belly Buster" always expected to be at index 1 or are there cases where that might not be true?

List<string> pizzas = new List<string>();
pizzas.Add("Angus Steakhouse");
pizzas.Add("Belly Buster");
pizzas.Add("Pizza Bianca");
pizzas.Add("Classic Cheese");
pizzas.Add("Friday Special");

string result = pizzas[1]; // result is equal to "Belly Buster"
  • yes it can change if you modify the list, such as deleting Angus – kenny Dec 10 '14 at 18:38
  • Here is another: http://stackoverflow.com/questions/453006/does-a-listt-guarantee-that-items-will-be-returned-in-the-order-they-were-adde or this http://stackoverflow.com/questions/1043039/does-listt-guarantee-insertion-order – Tim Schmelter Dec 10 '14 at 18:47

1 Answers1

0

index starts at 0 so yes if the code stays the same..belly buster will always be at 1

SuncoastOwner
  • 263
  • 2
  • 9
  • Not downvoted, but even if the index starts always with 0 the item at index 0 _could_ change. – Tim Schmelter Dec 10 '14 at 18:38
  • in the code provided...Belly Buster will always return index 1. – SuncoastOwner Dec 10 '14 at 18:42
  • 1
    Yes, but your explanation is not sufficient, it's like someone says that the earth is round because ... because it is so. A good way to answer such questions is to provide the relevant part of the language specification or at least MSDN. – Tim Schmelter Dec 10 '14 at 18:45
  • 1
    "Can someone please comment if the elements in the list are expected to retain order? In the example above, is "Belly Buster" always expected to be at index 1 or are there cases where that might not be true?" – SuncoastOwner Dec 10 '14 at 18:50
  • I would have added a reason / link to MSDN. This answer almost sounds facetious. Giving you the benefit of the doubt and up-voted. – djv Dec 10 '14 at 19:36