public void myCollection()
{
IEnumerable<string> vs;
ICollection<string> vs1; //获取集合中元素个数,从集合中删除增加元素Add()/Remove()/Clear()
IList<string> vs2; //继承自ICollection接口,同时增加了一个索引器,可以在指定位置插入或删除某些项Insert()/RemoveAt()
ISet<string> vs3; //继承自ICollection接口,
IDictionary<string, string> keyValuePairs;
ILookup<string, string> vs4; //类似于IDictionary,可以通过一个键包含多个值
IComparer<string> comparer;
IEqualityComparer<string> equalityComparer;
IProducerConsumerCollection<string> vs5; //线程安全的集合类
ArrayList arrayList = new ArrayList();
arrayList.Add("s");
//列表、数组
List list = new List();
List<string> vs6 = new List<string>(50);
vs6.TrimExcess(); //去除不需要的容量
vs6.RemoveAt(0);
int c = vs6.FindIndex(r => r.Length > 5);
vs6.Sort(); //默认快排
List<int> p = vs6.ConvertAll(r => r.Length); //数据类型转换
var s = vs6.AsReadOnly();
//队列、链表
Queue queue = new Queue();
queue.Enqueue("aaa");
queue.Dequeue();
object p1 = queue.Peek();
var c1 = queue.Count;
Queue<string> vs7 = new Queue<string>();
vs7.Enqueue("ss");
//栈
Stack stack = new Stack();
stack.Push(10);
stack.Pop();
//双向链表
LinkedList<string> vs8 = new LinkedList<string>();
vs8.AddFirst("ss");
vs8.AddFirst(new LinkedListNode<string>("first"));
vs8.AddAfter(new LinkedListNode<string>("ss"),"dd");
vs8.AddBefore(new LinkedListNode<string>("ss"), "dd");
vs8.AddBefore(vs8.Last, new LinkedListNode<string>("dd"));
vs8.AddLast("last");
vs8.Remove("dd");
//有序列表 数组和hash表的组合 按照键排序 如果使用索引访问各项,则他是一个动态数组,键访问各项,他是一个hashTable
SortedList<string, string> valuePairs = new SortedList<string, string>();
valuePairs.Add("h", "g");
//字典的性能取决于键的GetHashCode方法的实现代码,使hash表的索引位置尽可能的散列
Dictionary<string, string> keyValuePairs1 = new Dictionary<string, string>();
object ss = "dd";
int hash = ss.GetHashCode();
//LookUp类
var racers = new List<string>();
racers.AddRange(new List<string>(){ "dd","da","ccc"});
ILookup<int, string> lookupRacers = racers.ToLookup(r=>r.Length);
foreach(string ll in lookupRacers[2])
{
Console.WriteLine("查找到key为2时的value值", ll);
}
//SortedDictionary 底层是二叉树 有序字典 和SortedList类似
//包含不重复元素的集合成为"集set"
var companyTeam = new HashSet<string>() { "dd", "a", "d" };
Dictionary<int, string> len = companyTeam.ToDictionary(r => r.Length);
companyTeam.IsSubsetOf(new HashSet<string>() { "d" }); //是否包含子集
companyTeam.IsProperSubsetOf(new HashSet<string>() { "dd", "a", "d" }); //是否包含真子集
//集合中的元素何时删除或添加信息,使用ObservableCollection<T>类
System.Collections.ObjectModel.ObservableCollection<string> vs9 = new System.Collections.ObjectModel.ObservableCollection<string>();
}
ObservableCollection 和BindingList
https://stackoverflow.com/questions/4284663/difference-between-observablecollection-and-bindinglist
在UI界面上使用双向绑定的时候最好使用ObservableCollection,因为他实现了INotifyPropertyChanged和INotifyCollectionChanged的方法。
<Grid>
<StackPanel Height="295" HorizontalAlignment="Left" Margin="10,10,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="427">
<TextBlock Height="23" Name="textBlock1" Text="学员编号:" />
<TextBox Height="23" Name="txtStudentId" Width="301" HorizontalAlignment="Left"/>
<TextBlock Height="23" Name="textBlock2" Text="学员列表:" />
<ListBox Height="156" Name="lbStudent" Width="305" HorizontalAlignment="Left">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Name="stackPanel2" Orientation="Horizontal">
<TextBlock Text="{Binding Id,Mode=TwoWay}" Margin="5" Background="Beige"/>
<TextBlock Text="{Binding Name,Mode=TwoWay}" Margin="5"/>
<TextBlock Text="{Binding Age,Mode=TwoWay}" Margin="5"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Button" Height="23" Name="button1" Width="75" HorizontalAlignment="Left" Click="button1_Click" />
</StackPanel>
</Grid>
public test()
{
InitializeComponent();
//this.lbStudent.ItemsSource = infos;
//this.lbStudent.ItemsSource = infos2;
this.lbStudent.ItemsSource = infos3;
this.txtStudentId.SetBinding(TextBox.TextProperty, new Binding("SelectedItem.Id") { Source = lbStudent });
}
ObservableCollection<Students> infos = new ObservableCollection<Students>() {
new Students(){ Id=1, Age=11, Name="Tom"},
new Students(){ Id=2, Age=12, Name="Darren"},
new Students(){ Id=3, Age=13, Name="Jacky"},
new Students(){ Id=4, Age=14, Name="Andy"}
};
List<Students> infos2 = new List<Students>()
{
new Students(){ Id=1, Age=11, Name="Tom"},
new Students(){ Id=2, Age=12, Name="Darren"},
new Students(){ Id=3, Age=13, Name="Jacky"},
new Students(){ Id=4, Age=14, Name="Andy"}
};
BindingList<Students> _infos3 = new BindingList<Students>()
{
new Students(){ Id=1, Age=11, Name="Tom"},
new Students(){ Id=2, Age=12, Name="Darren"},
new Students(){ Id=3, Age=13, Name="Jacky"},
new Students(){ Id=4, Age=14, Name="Andy"}
};
public event PropertyChangedEventHandler PropertyChanged;
BindingList<Students> infos3 { get => _infos3; set { _infos3 = value;PropertyChanged?.Invoke(this,new PropertyChangedEventArgs("infos3")); } }
private void button1_Click(object sender, RoutedEventArgs e)
{
infos[1] = new Students() { Id = 4, Age = 14, Name = "这是一个集合改变" };
infos[2].Name = "这是一个属性改变";
infos2[1] = new Students() { Id = 4, Age = 14, Name = "这是一个集合改变" };
infos2[2].Name = "这是一个属性改变";
infos3[1] = new Students() { Id = 4, Age = 14, Name = "这是一个集合改变" };
infos3[2].Name = "这是一个属性改变";
}
public class Students : INotifyPropertyChanged
{
string _name;
public int Id { get; set; }
public string Name
{
get { return _name; }
set { _name = value; OnPropertyChanged("Name"); }
}
public int Age { get; set; }
protected internal virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}