. NET basic set,. net set

Source: Internet
Author: User

. 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;        }    }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.