. NET basic set,. net set
A set can be a superset of arrays. A set can maintain an array of objects. A set contains more advanced functions. For example, you can control the access, search, and sorting of the objects it contains. The array is fixed. Once an array is created, a new item cannot be added to the end of the existing array unless we create a new array.
Arrays in C # are implemented as instances of the System. Array class. They are only one of the Collection classes. The functions of the Collection class are obtained by implementing interfaces in the System. Collection namespace.
Several Interfaces in the System. Collection namespace provide a basic set of functions:
- IEnumerable can iterate items in the set. The unique method GetEnumerator () in this interface can iterate the items in the set. This method must be implemented for objects using the foreach Structure
- ICollection (Inheritance and IEnumerable) can obtain the number of items in the set and copy the items to a simple array type. The ICollection includes the CopyTo (System. Array array, int index) method and Count attribute.
- IList (inherited from IEnumberable and ICollection) provides a list of items in the Set, and can access these items and other functions related to the item list. Public class Animal {// class member}
Set of implementation:
Public class Animals : CollectionBase{ public void Add(Animal newAnimal) { List.Add(newAnimal); } public void Remove(Animal oldAnimal) { List.Remove(oldAnimal); } public Animals() { }}
If you want to access through an index, you need to add an index. Add the index as follows:
public class Animals : CollectionBase{ ... public Animal this[int animalIndex] { get { return (Animal)List[animalIndex]; } set { List[animalIndex] = value; } }}
Custom Dictionary, which can be accessed through keywords
It is very similar to the implementation example of the previous set, but there are still some differences. To implement the IDictionary interface, we can inherit DictionaryBase. The details are as follows:
public class Animals : DictionaryBase{ public void Add(string newID, Animal newAnimal) { Dictionary.Add(newID, newAnimal); } public void Remove(string oldID) { Dictionary.Remove(oldID); } public Animals() {} public Animal this[string animalID] { get { return (Animal)Dictionary[animalID]; } set { Dictionary[animalID] = value; } }}