1. 程式人生 > >ObservableCollection和List的區別總結

ObservableCollection和List的區別總結

con 動態 操作 enume 事件 運行 表示 類的方法 更新

一、繼承的類和接口,還有它們的方法不同

1)ObservableCollection比較簡單,繼承了Collection, INotifyCollectionChanged, INotifyPropertyChanged

  Collection:為泛型集合提供基類。

  INotifyCollectionChanged:將集合的動態更改通知給偵聽器,例如,何時添加和移除項或者重置整個集合對象。

  INotifyPropertyChanged:向客戶端發出某一屬性值已更改的通知。

  所以再ObservableCollection這個類的方法,對數據的操作很少,重點放在了當自己本事變化的時候(不管是屬性,還是集合)會調用發出通知的事件。(一般用於更新UI,

  當然也可以用於寫其他的事情。這個以後會寫)

2)List就比較多了,繼承了IList, ICollection, IEnumerable, IList, ICollection, IEnumerable。

  IList:表示可按照索引單獨訪問的一組對象。

  ICollection:定義操作泛型集合的方法。

  IEnumerable:公開枚舉器,該枚舉器支持在指定類型的集合上進行簡單叠代。

  IList:表示可按照索引單獨訪問的對象的非泛型集合。

  ICollection:定義所有非泛型集合的大小、枚舉器和同步方法。

  IEnumerable:公開枚舉器,該枚舉器支持在非泛型集合上進行簡單叠代。

DEMO

 1 <ListBox x:Name="listbind" Height="61" HorizontalAlignment="Left" Margin="146,12,0,0" VerticalAlignment="Top" Width="120" >  
 2             <ListBox.ItemTemplate>  
 3                 <DataTemplate>  
 4                     <TextBlock Text="{Binding Name}" />  
 5
</DataTemplate> 6 </ListBox.ItemTemplate> 7 </ListBox> 8 <ListBox x:Name="observbind" Height="74" HorizontalAlignment="Left" Margin="146,111,0,0" VerticalAlignment="Top" Width="120" > 9 <ListBox.ItemTemplate> 10 <DataTemplate> 11 <TextBlock Text="{Binding Name}" /> 12 </DataTemplate> 13 </ListBox.ItemTemplate> 14 </ListBox> 15 <TextBlock Height="23" HorizontalAlignment="Left" Margin="38,58,0,0" Name="textBlock1" Text="List綁定數據" VerticalAlignment="Top" /> 16 <TextBlock Height="44" HorizontalAlignment="Left" Margin="12,125,0,0" Name="textBlock2" Text="ObservableCollection綁定數據" VerticalAlignment="Top" Width="112" /> 17 <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="77,211,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />

xaml頁面很簡單,托2個listbox分別用來綁定ObservableCollection和List

1 public class Person  
2     {  
3         public string Name { get; set; }  
4     }  

實體類:

 1      private List<Person> person1 = new List<Person>();  
 2         private ObservableCollection<Person> person2 = new ObservableCollection<Person>();  
 3   
 4         public DemoTestDiff()  
 5         {  
 6             InitializeComponent();  
 7             person1.Add(new Person() { Name = "張三" });  
 8             person1.Add(new Person() { Name = "李四" });  
 9             listbind.ItemsSource = person1;  
10             person2.Add(new Person() { Name = "張三" });  
11             person2.Add(new Person() { Name = "李四" });  
12             observbind.ItemsSource = person2;  
13         }  
14   
15         private void button1_Click(object sender, RoutedEventArgs e)  
16         {  
17             person1.Add(new Person() { Name = "王五" });  
18             person2.Add(new Person() { Name = "王五" });  
19         }  

運行程序點擊button按鈕,然後只有ObservableCollection的有添加。

表示當集合對象的集合改變時,只有ObservableCollection會發出通知更新UI。

這只是他們兩個區別之一。

綜上所述:

   ObservableCollection表示一個動態數據集合,在添加項、移除項或刷新整個列表時,此集合將提供通知。

   List表示可通過索引訪問的對象的強類型列表。提供用於對列表進行搜索、排序和操作的方法。(大部分操作用Linq,很強大也很方便。)

ObservableCollection和List的區別總結