我在一個項目上工作,我發現這個人使用了一段代碼。如果是我,我會使用一個簡單的數組,因爲這是我所知道的。查看下面的代碼,選擇列表對數組有什麼好處?爲什麼選擇數組列表?
List<string> errors = new List<string>();
if (ddDirector.SelectedItem.Value == "")
errors.Add("You must select an item in the Director list.");
if (errors.Count > 0)
{
ErrorList.InnerHtml = "Please correct the following issues below:<br/><ul>";
foreach (string e in errors)
{
ErrorList.InnerHtml += String.Format("<li>{0}</li>", e);
}
ErrorList.InnerHtml += "</ul>";
}
return (errors.Count()==0);
列表中有一些方法可以方便地在給定的位置或從開始或結束以及其他方法中添加和減去元素。 https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx – ProgrammingDude
老實說,根據我的經驗,人們使用'List'因爲它們很懶,很容易使用。儘管從性能的角度來看,並不總是最好的選擇。 –
CodingGorilla
如果你不確定項目的數量,然後去列表。 – Arshad