C#之Collection类的学习
学习.Net常用基类之Collection<>类的常用属性和方法的学习和继承Collection<>类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
/// <summary>
/// This section contains two code examples.The first example demonstrate several properties and
/// methods of Collection<T>.The second example shows how to derive a collection class from a
/// constrcuted typed of Collection<T>,and how to override the protected methods of Collection<T>
/// to provide custom behavior.
/// </summary>
namespace 练习基类
{
class Program
{
static void Main(string[] args)
{
#region Example 1
// The following code exmaple demonstrate many of the properties and methods of Collection<T>.The code
// example creates a collection of string,uses the Add method to add several strings,displays the count,and
// lists the string.
//Collection<string> subjets = new Collection<string>();
//subjets.Add("English");
//subjets.Add("Math");
//subjets.Add("PE");
//subjets.Add("COmputer");
//subjets.Add("Calculas");
//Console.WriteLine($"the number of subjects is:{subjets.Count}");
//Display(subjets);
#endregion
#region Example 2
// The following code demonstrate how to derive a collection class from a constrcuted typed of
// the Collection<T> class.And how to override the protected method InsertItem,RemoveItem,
// ClearItems and SetItem methods to provide custom behavior for the Add,Insert,and Clear
// methods,and for the setting item property.
Dinosaurs dinosaurs = new Dinosaurs();
dinosaurs.Changed += ChangedHandler;
dinosaurs.Add("Psitticosaurus");
dinosaurs.Add("Caudipteryx");
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Muttaburrasaurus");
Display(dinosaurs);
Console.WriteLine("\nIndexOf(\"Muttaburrasaurus\"): {0}",
dinosaurs.IndexOf("Muttaburrasaurus"));
Console.WriteLine("\nContains(\"Caudipteryx\"): {0}",
dinosaurs.Contains("Caudipteryx"));
Console.WriteLine("\nInsert(2, \"Nanotyrannus\")");
dinosaurs.Insert(2, "Nanotyrannus");
Console.WriteLine("\ndinosaurs[2]: {0}", dinosaurs[2]);
Console.WriteLine("\ndinosaurs[2] = \"Microraptor\"");
dinosaurs[2] = "Microraptor";
Console.WriteLine("\nRemove(\"Microraptor\")");
dinosaurs.Remove("Microraptor");
Console.WriteLine("\nRemoveAt(0)");
dinosaurs.RemoveAt(0);
Display(dinosaurs);
#endregion
Console.ReadKey();
}
private static void ChangedHandler(object sender, DinosaursChangedEventArgs e)
{
if(e.changedType==ChangedType.Replaced)
{
Console.WriteLine($"{e.ChangedItem} was replaced with {e.ReplaceWith}");
}
else if(e.changedType==ChangedType.Cleared)
{
Console.WriteLine("the dinosaurs list was cleared");
}
else
{
//Console.WriteLine("{0} was {1}.", e.ChangedItem, e.ChangeType);
}
}
/// <summary>
/// Display members of a Collection
/// </summary>
/// <param name="strs"></param>
public static void Display(Collection<string> strs)
{
foreach(string str in strs)
{
Console.WriteLine(str);
}
}
}
public class Dinosaurs:Collection<string>
{
public event EventHandler<DinosaursChangedEventArgs> Changed;
protected override void InsertItem(int index, string item)
{
base.InsertItem(index, item);
EventHandler<DinosaursChangedEventArgs> temp = Changed;
if(temp!=null)
{
temp(this, new DinosaursChangedEventArgs(ChangedType.Added, item, null));
}
}
protected override void SetItem(int index, string item)
{
string replaced = Items[index];
base.SetItem(index, item);
EventHandler<DinosaursChangedEventArgs> temp = Changed;
if(temp!=null)
{
temp(this, new DinosaursChangedEventArgs(ChangedType.Replaced, replaced, item));
}
}
protected override void ClearItems()
{
base.ClearItems();
EventHandler<DinosaursChangedEventArgs> temp = Changed;
if (temp != null)
{
temp(this, new DinosaursChangedEventArgs(ChangedType.Remove, null, null));
}
}
protected override void RemoveItem(int index)
{
string removed = Items[index];
base.RemoveItem(index);
EventHandler<DinosaursChangedEventArgs> temp = Changed;
if (temp != null)
{
temp(this, new DinosaursChangedEventArgs(ChangedType.Remove, removed, null ));
}
}
}
// Event argument for the changed event
public class DinosaursChangedEventArgs:EventArgs
{
public readonly string ChangedItem;
public readonly ChangedType changedType;
public readonly string ReplaceWith;
public DinosaursChangedEventArgs(ChangedType change,string item,string replacement )
{
this.changedType = change;
this.ChangedItem = item;
this.ReplaceWith = replacement;
}
}
public enum ChangedType
{
Added,
Remove,
Replaced,
Cleared
};
}