1. Introduction
Offical Website : learn.microsoft.com/en-us/dotne…
1.Properties
| Count |
Gets the number of elements actually contained in the Collection<T>. |
| Item[Int32] |
Gets or sets the element at the specified index. |
| Items |
Gets a IList<T> wrapper around the Collection<T>. |
2.Method
| Add(T) |
Adds an object to the end of the Collection<T>. |
|
Block |
Disallows reentrant attempts to change this collection. |
|
Check |
Checks for reentrant attempts to change this collection. |
| Clear() |
Removes all elements from the Collection<T>. |
|
Clear |
Removes all items from the collection. |
| Contains(T) |
Determines whether an element is in the Collection<T>. |
|
Copy |
Copies the entire Collection<T> to a compatible one-dimensional Array, starting at the specified index of the target array. |
| Equals(Object) |
Determines whether the specified object is equal to the current object. |
|
Get |
Returns an enumerator that iterates through the Collection<T>. |
|
Get |
Serves as the default hash function. |
|
Get |
Gets the Type of the current instance. |
|
Index |
Searches for the specified object and returns the zero-based index of the first occurrence within the entire Collection<T>. |
| Insert(Int32, T) |
Inserts an element into the Collection<T> at the specified index. |
|
Insert |
Inserts an item into the collection at the specified index. |
|
Memberwise |
Creates a shallow copy of the current Object. |
| Move(Int32, Int32) |
Moves the item at the specified index to a new location in the collection. |
|
Move |
Moves the item at the specified index to a new location in the collection. |
|
On |
Raises the CollectionChanged event with the provided arguments. |
|
On |
Raises the PropertyChanged event with the provided arguments. |
| Remove(T) |
Removes the first occurrence of a specific object from the Collection<T>. |
|
Remove |
Removes the element at the specified index of the Collection<T>. |
|
Remove |
Removes the item at the specified index of the collection. |
|
Set |
Replaces the element at the specified index. |
|
To |
Returns a string that represents the current object. |
2. Basic Method
1.Foreach
public ObservableCollection<Student> datalist = new ObservableCollection<Student>();
public void PrintEvery()
{
foreach (Student student in datalist)
{
System.Diagnostics.Trace.WriteLine("---------element:" + student);
}
}
2.Add
public ObservableCollection<Student> datalist = new ObservableCollection<Student>();
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
Student s = new Student();
s.SID = txtid.Text;
s.SName = txtname.Text;
s.SAddress = txtaddress.Text;
datalist.Add(s);
}
3.Update
public ObservableCollection<Student> datalist = new ObservableCollection<Student>();
private void btnUpdate_Click(object sender, RoutedEventArgs e)
{
foreach (var item in datalist)
{
if (item.SID == txtid.Text)
{
item.SName = txtname.Text;
item.SAddress = txtaddress.Text;
}
}
}
4.Delete
public ObservableCollection<Student> datalist = new ObservableCollection<Student>();
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
var ca = (Student)this.dgStudent.SelectedItem;
datalist.Remove(ca);
}