Basic knowledge of C #: Basics (ICollection), iterations, and generics

Source: Internet
Author: User
1. ICollection Interface
Arrays are the most basic collection types defined by the. NET Framework, and in addition to arrays, the. NET Framework defines a number of collection types. When you open MSDN, you see that all collections are under the System.Collections namespace:
Collection class:
ArrayList: Implements the IList interface using an array of size that can be dynamically added on demand.
BitArray: A compressed array of managed values, represented as a Boolean value, true to indicate that the bit is open (1), and false to indicate that the bit is closed (0).
CaseInsensitiveComparer: Compares two objects for equality, ignoring the case of strings when compared.
CollectionBase: Provides an abstract base class for a strongly typed collection.
Comparer: Compares two objects for equality, where string comparisons are case-sensitive.
DictionaryBase: Provides an abstract base class for a strongly typed collection of key/value pairs
Hashtable: Represents a collection of key/value pairs that are organized according to the hash code of the key.
Queue: Represents an object's first in and out collection
ReadOnlyCollectionBase: Provides an abstract base class for strongly typed non-generic read-only collections.
SortedList: Represents a collection of key/value pairs that are sorted by keys and accessed by key and index.
Stack: Represents a simple, LIFO, non-generic collection of objects.
Interface:
ICollection: Defines the size, enumerator, and synchronization method for all non-generic collections.
IComparer: Exposes a method that compares two objects.
IDictionary: A non-generic collection that represents a key/value pair.
IDictionaryEnumerator: Enumerates the elements of a non-generic dictionary.
IEnumerable: Exposes an enumerator that supports simple iterations on a non-generic collection.
IEnumerator: Supports simple iterations of non-generic collections.
IEqualityComparer: Defines methods to support equality comparisons of objects.
IList: Represents a non-generic collection of objects that can be accessed individually by index.
Mainly look at the ICollection interface and the IEnumerable interface.
ICollection:
The interface defines two main specifications for its implementation class:
(1) The number of collection elements, that is, the Count property;
(2) Iteration (GetEnumerator method).
The Getenumertor method is inherited by ICollection's parent interface ieumerable. The ICollection interface defines a collection of objects that store and get object types, which can store and get reference or value type objects of various reference type objects.
IEnumerable Interface:
This interface is the parent interface of ICollection, which provides the ability to iterate over the classes it implements. The Ieumerable interface has only one GetEnumerator method, which returns an enumerator that iterates through the collection
2. Iteration
Iterations, also called iterators, are iterative patterns in design patterns, meaning that they provide a way to sequentially access individual elements of an aggregated object without exposing the object's internal representation. Because collections are stored in different ways, list is continuous storage, and linked lists use reference storage between objects ... Then, when it is inconvenient to traverse the collection, you need a way to traverse all the different collections, and there is an iterator.
Iterators consider the combination of classes as a container of infinite capacity, and the storage rules are linear. That is, in addition to the first and last each element has a direct precursor and a direct successor, the container has a flag that can point to any storage unit in the container. There are two ways: one-way iterations and two-way iterations.
(1), one-way iteration
One-way iterations refer to only forward iterations, and the set of cell flags can only be moved backwards from one element of the collection, so the iterations define three actions:
A, current item
b, to the next item and determine whether the end
C, back to the initial item

(2), bidirectional iterative mode
For a bidirectional iteration pattern, the cell flags of the collection move backwards from one item of the collection to the other, and can move forward one item at a set, so there are four actions:
A, current item
B, advance to the next item and determine whether the end
C, go back to the previous item and decide whether it is the beginning of the collection
D, return to the initial position

The. NET Framework uses the Ieumerable interface to define a one-way iterative specification. The ICollection interface inherits the Ieumerable interface, which defines the GetEnumerator method to get an iterative object of the current collection class object, an object that implements the Ieumerable interface.
Any class that implements the IEnumerable interface can iterate through a foreach loop.
The IEnumerator interface defines an iteration that iterates through object references, and the IEnumerable interface defines a way to get this iterative sub-object.
3. Generic Collection
Icollection<t> interface
The Icollection<t> interface defines a generic collection interface, the so-called generic collection, that uses the generic attributes provided by the. NET Framework to qualify a collection to store only a collection of data of a particular type of object reference or value type. Generic collections enhance the efficiency and security of collection access.
Because the storage type has been determined, icollection<t> provides more methods than the ICollection interface, including adding to the collection, removing from the collection, and judging whether an object reference (or object value) exists in the collection.
Ienumerable<t> interface
The Icollection<t> interface implements the Ienumerable<t> interface, which is used primarily to return an iterative sub-object that implements the Ienumerator<t> interface to determine the type.

The above is the basic knowledge of C #: Basic knowledge (ICollection), iterative and generic content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.