Let's take a look at the difference between C# ObservableCollection and List

Talking about the methods of the C# ObservableCollection and ListObservableCollection classes, there are very few operations on the data. The focus is on the event that will be called when the ability changes (whether it is a property or a collection).

Let's take a look at the difference between C# ObservableCollection and List Let's take a look at the difference between C# ObservableCollection and List

1. The difference between ObservableCollection and List

1) ObservableCollection is relatively simple, inheriting Collection, INotifyCollectionChanged, INotifyPropertyChanged

Collection: Provides a base class for generic collections.

INotifyCollectionChanged: Notify the listener of dynamic changes to the collection, for example, when to add and remove items or reset the entire collection object.

INotifyPropertyChanged: Notify the client that a certain property value has changed.

Therefore, the methods of ObservableCollection have very few operations on data. The focus is on the event that will be called to notify when one's ability changes (whether it is a property or a collection). (Generally used to update the UI, of course, it can also be used to write other things. This will be written later)

2) List is more, inherited IList, ICollection, IEnumerable, IList, ICollection, IEnumerable.

IList: Represents a group of objects that can be individually accessed by index.

ICollection: Define methods for operating generic collections.

IEnumerable: Public enumerator that supports simple iteration on a collection of a specified type.

IList: Represents a non-generic collection of objects that can be individually accessed by index.

ICollection: Defines the size, enumerator, and synchronization method of all non-generic collections.

IEnumerable: Public enumerator that supports simple iteration on non-generic collections.

Two, for example:

1. Example 1:

MainWindow.xaml:

The xaml page is very simple. Two listboxes are used to bind ObservableCollection and List
Person.cs:

public class Person   
     { 
         public string Name { get; set; } 
     } 

MainWindow.xaml.cs:

private List person1 = new List(); 
private ObservableCollection person2 = new ObservableCollection(); 
public DemoTestDiff()   
{ 
InitializeComponent(); 
person1.Add(new Person() { Name = "张三" }); 
person1.Add(new Person() { Name = "李四" }); 
listbind.ItemsSource = person1; 
person2.Add(new Person() { Name = "张三" }); 
person2.Add(new Person() { Name = "李四" }); 
observbind.ItemsSource = person2; 
} 
private void button1_Click(object sender, RoutedEventArgs e)   
{ 
person1.Add(new Person() { Name = "王五" }); 
person2.Add(new Person() { Name = "王五" }); 
} 

Run the program and click the button button, and then only the ObservableCollection has been added.
Indicates that when the collection of collection objects changes, only ObservableCollection will send a notification to update the UI.

This is just one of the differences between the two of them.

2. Example 2

The following methods can update the UI of the ListView:

private ObservableCollection<PreviewListModel> _previewList = new ObservableCollection<PreviewListModel>();
/// <summary>
/// 预览信息列表
/// </summary>
public ObservableCollection<PreviewListModel> PreviewList
{
get { return _previewList; }
set { SetProperty(ref _previewList, value); }
//set { _previewList = value; RaisePropertyChanged("PreviewList"); }
}

Three, the mutual conversion of ObservableCollection and List

The collection retrieved from the database is of type List, what should we do if we need to convert it to ObservableCollection type? The following method:

T tList = new List(tObjectStruct .ToList()); 
ObservableCollection tObjectStruct  = new  

Database search:

public void AdvancedSearchFunc(AdvancedSearchNotification advancedSearchNotification) 
{ 
try 
{ 
KrayMobileDREntities dataBase = new KrayMobileDREntities(); 
//must be cleared before each use 
patientInfoHistroryModel.Clear(); 
//First extract the data from the database and put it in the collection. 
List<PatientInfo_Table> patientInfoList = 
dataBase.PatientInfo_Table.Where(u => u.PatientKey.ToString().Equals(advancedSearchNotification.PatientInfo) 
|| u.PatientID.ToString().Equals(advancedSearchNotification.StudyID) 
|| u.PatientName .ToString().Equals(advancedSearchNotification.PatientName) 
).ToList(); 
List<PatientStudy_Table> patientStudyList = dataBase.PatientStudy_Table.Where(u => u.PatientKey <10).ToList(); 
//Retrieve the collection by condition
List<PatientInfoHistroryModel> list =
(from pI in patientInfoList
where (pI.PatientKey < 1000)
select new PatientInfoHistroryModel()
{
PatientInfo = pI.PatientKey.ToString(),
StudyID = pI.PatientID.ToString(),
PatientName = pI.PatientName.ToString(),
PatientSex = pI.PatientSex.ToString(),
PatientAge = pI.PatientAge.ToString(),
PatientBrith = pI.PatientBirthDate.ToString(),
PatientHeight = pI.PatientHeight.ToString(),
PatientWeight = pI.PatientWeight.ToString(),
RecordSource = pI.PatientSource.ToString(),
//StudyTime       = PS.StudyDatetime,
//EquipmentType   = PS.StudyPhysician,
//StudyPart       = PS.StudyType,
//SequenceAmount = PS.SeriesCount, 
StudyTime = pI.PatientAge.ToString(), 
EquipmentType = pI.PatientAge.ToString(), 
StudyPart = pI.HangFlag.ToString(), 
SequenceAmount = pI.HangFlag.ToString(), 
StudyStutas = pI.StudyCompleteFlag.ToString(), 
SuspendState = pI.HangFlag.ToString(), 
FilmPrint = pI.PrintFlag.ToString(), 
}).ToList(); 
patientInfoHistroryModel = list; 
dataBase.Dispose(); 
} 
catch (Exception ex ) 
{ 
MessageBox.Show("Patient history information table [Advanced query] state, a database error occurred. Error message: --------------" + ex.ToString()); 
LogHelper .Error("OperateDataSheetViewModel.cs::AdvancedSearchFunc() advanced query failed--" + ex.Message); 
} 
}

Four, summary

1. ObservableCollection represents a dynamic data collection. This collection will provide notifications when adding items, removing items, or refreshing the entire list.
2. List represents a strongly typed list of objects that can be accessed by index. Provides methods for searching, sorting, and manipulating lists. (Linq is used for most operations, which is powerful and convenient.

Guess you like

Origin blog.csdn.net/yaxuan88521/article/details/113857598
Recommended