選擇
private List<Foo> myOtherList = new List<Foo>();
becuse另一隻聲明瞭一個參考(其被設置爲空),上面的示例聲明到列表的引用,創建一個列表和該assignes新列表到參考。
選擇
public List<Foo> ListOfFoo
{
get { return myList; }
set { myList= new List<Foo>(value); }
}
當你想myList中爲NOT refelect它被分配給myList中如後發生在列表中的任何改變
List<string> myFirstList = new List<string>();
myFirstList.Add("Hello");
myFirstList.Add("World");
List<string> mySecondList = new List<string>(myFirstList);
// mySecondList now contains Hello & world
myFirstList.Add("Boyo");
// myFrist List now contains Hello, world & Boyo
// mySecondList still contains Hello & world
選擇
public List<Foo> ListOfFoo
{
get { return myList; }
set { myList= value; }
}
當你想兩個引用指向同一個對象如
List<string> myFirstList = new List<string>();
myFirstList.Add("Hello");
myFirstList.Add("World");
List<string> mySecondList = myFirstList;
// mySecondList now contains Hello & world
myFirstList.Add("Boyo");
// myFrist List now contains Hello, world & Boyo
// mySecondList "also" contains Hello, world & Boyo
的「也」以上是引號,因爲實際上,只有一個列表,我的兩個第一和第二點,以相同的列表。
什麼原因可以從外部設置收集? – peterchen 2009-05-20 13:45:44
該屬性正在進行自定義控件,該控件允許僅爲列表中的Foo對象拖放功能。我打算查詢數據庫以獲取對象列表。每個控件可以有相同或不同的列表。 – Billy 2009-05-20 14:17:48