Data structure and algorithm 2016-06-03

Source: Internet
Author: User

1. Recursion

An algorithm calls itself to do part of its work, and in solving certain problems, an algorithm needs to call itself. If an algorithm calls itself directly or calls itself indirectly, the algorithm is called recursive. Depending on the method of invocation, it is divided into direct recursion and indirect recursion.

A recursive algorithm must consist of two parts: the initial part and the recursive part. The initial section deals only with simple inputs that can be resolved directly without having to re-call recursively. The recursive section contains one or more recursive calls to the algorithm, and each invocation parameter is somewhat closer to the initial condition than the original invocation parameter.

2. Interface

The common language runtime (CLR) supports single-implementation inheritance and multi-interface inheritance.

Single-implementation inheritance means that a type can have only one base type. Multi-Interface inheritance refers to the ability of a type to inherit multiple interfaces, which is an abstraction of the interaction between classes, and the abstraction of the content that requires interaction between classes is defined as an interface, which provides better control over the logical interaction between classes. It can be seen that the abstraction of interface content is related to the quality of the whole program logic. It is also possible to add new functionality at any time by developing additional interfaces and implementations.

An important concept about interfaces is that the interface contains only member definitions and does not contain the implementation of the members. An interface is simply an abstract type that contains a set of virtual methods. The implementation of a member needs to be implemented in an inherited class or struct. The members of an interface include static methods, indexers, constants, events, and static constructors, etc., and do not contain any instance resources or instance constructors, so an interface cannot be instantiated.

3. Common interfaces

(1) IComparable interface

The IComparable interface defines a common comparison method. The IComparable interface used by the type provides a standard way to compare multiple objects. If a class is to implement a comparison with other objects, the IComparable interface must be implemented. By types that can be sorted, such as value type implementations, to create a comparison method that is specific to the type of destination that is appropriate for sorting.

(2) IEnumerable interface

The IEnumerable interface exposes an enumerator that supports simple iterations on the collection.

The IEnumerable interface can be implemented by classes that support iterating over content objects.

(3) IEnumerator interface

The IEnumerator interface supports simple iterations on the collection. is the base interface for all enumerators. Enumerators allow only the data in the collection to be read, and enumerators cannot be used to modify the underlying collection.

(4) ICollection interface

The ICollection interface defines the size, enumerator, and synchronization methods for all collections. The ICollection interface is the base interface for classes in the System.Collections namespace.

(5) IDictionary interface

The IDictionary interface is a more specialized interface based on the ICollection interface. The IDictionary interface implementation is a collection of key-value pairs, such as the Hashtable class.

(6) IList interface

The IList interface implementation is a combination of values that can be sorted and accessed by the index, such as the ArrayList class.

4. Interfaces and abstract classes

Abstract classes and interfaces have many similarities in definition and functionality, and choosing whether to use an abstract class or an interface in a program requires comparing the specific differences between an abstract class and an interface.

An abstract class is a class that cannot be instantiated and must inherit from it, and an abstract class can provide an implementation, or it may not provide an implementation. Subclasses can inherit from only one abstract class. Abstract classes should be used primarily for closely related objects. If you want to design a large functional unit or create multiple versions of a component, use an abstract class.

An interface is a fully abstracted collection of members and does not provide implementations where a class or struct can inherit multiple interfaces. Interfaces are best suited to provide common functionality for unrelated classes. If you want to design a small and concise function block, use the interface. Once an interface is created, it cannot be changed, and if a new version of the interface is required, a completely new interface must be created.

5. Implementation of the interface

The implementation of the interface is divided into implicit implementation and explicit implementation. If a class or struct is to implement a single interface, you can use an implicit implementation, and if the class or struct inherits multiple interfaces, the same name member in the interface is explicitly implemented. The display implementation is implemented by using the fully qualified name of the interface to implement the interface member.

Data structure and algorithm 2016-06-03

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.