About the C # division type,

Source: Internet
Author: User

About the C # division type,

Waiting for the Lantern Festival to arrive, after the Lantern Festival is over, this year is also over, and you have to start earning money. When you go home, you feel that everyone is very good, only you are too scum, to avoid this embarrassing situation at the end of the year, we still need to work hard. Strive to become CEO, win Bai fumei, and embark on the peak of life. (Life requires fantasies, but also the face of reality. It is more difficult to get an upward mentality than to possess wealth .)

Partial class SegmentType {private void Add () {Update ();} private void Delete (){}}

SegmentType2.cs

  partial class SegmentType    {        private void Update()        {            Delete();        }    }

The above is a simple declaration and application of the division type. These two. CS files have been merged before the compiler compilation. For the Division type, you cannot write half of the code of a Member in one file, but put the other half of the Code into another file, make sure that each independent Member is in the file where it is located. The following code:

SegmentType1.cs:

    partial class SegmentType    {        private void Add(string fileName)        {            FileStream fs = null;            try            {                fs = File.Create(fileName);            }                   }    }

SegmentType2.cs

 partial class SegmentType    {        private void Update()        {            Add();            catch (ArgumentException arex)            {                throw arex;            }            finally            {                if (fs != null)                {                    fs.Close();                    fs.Dispose();                }            }        }    }

The method demonstrated above cannot be compiled.

There are some restrictions on the Type Declaration, that is, the declaration must be compatible with each other. Any file can specify the interface and the base type to be implemented, and the type parameter constraints. If the base types are set for multiple files, they must be the same, and if the type parameter constraints are set for multiple files, the constraints must be consistent. The following code example is provided:

SegmentType1.cs:

// Interface constraint: IEquatable <string>; // where TFirst: class: And type parameter constraint partial class SegmentType <TFirst, TSecond>: IEquatable <string> where TFirst: class {// implement IEquatable <string> interface method public bool Equals (string other) {return false ;}}

SegmentType2.cs

// Specify the base class: EventArgs // specify the interface: IDisposable partial class SegmentType <TFirst, TSecond>: EventArgs, IDisposable {// implementation method public void Dispose (){}}

You can also use the following methods in the preceding interface and base class constraints:

SegmentType1.cs:

// Interface constraint: IEquatable <string>; // where TFirst: class: And type parameter constraint partial class SegmentType <TFirst, TSecond>: IEquatable <string> where TFirst: class {// implementation method public void Dispose (){}}

SegmentType2.cs

// Specify the base class: EventArgs // specify the interface: IDisposable partial class SegmentType <TFirst, TSecond>: EventArgs, IDisposable {// implement IEquatable <string> interface method public bool Equals (string other) {return false ;}}

Interfaces and primitive type constraints can be exchanged. Based on this feature, you can separate specified interfaces from implementations and encapsulate methods that generate the same signature for different types into one interface. This interface cannot be specified when the type is declared.

The above section mainly describes how to create and use the branch type. Next, let's take a look at the knowledge of the branch method.

3. Analysis of the division method:

The concept of the division method has been introduced before. the creation and use of the division method is similar to that of the division type. The division method has one feature: any call to an unimplemented division method will be removed by the compiler.

The Declaration of the partial method is similar to that of the abstract method. You only need to use the partial modifier to provide the signature without any implementation. The implementation also requires the partial modifier. The following code is available:

SegmentType1.cs:

Partial class SegmentType {public SegmentType () {SegmentTypeStart (); Console. writeLine ("segment method parsing... "); SegmentTypeEnd ();} partial void SegmentTypeStart (); partial void SegmentTypeEnd ();}

SegmentType2.cs

Partial class SegmentType {partial void SegmentTypeStart () {Console. WriteLine ("division method start ...");}}

In the partial method, because the method may not exist, the return type of the partial method must be declared as void and the out parameter cannot be obtained. The division method must be private, but static or generic.

II. C # features of the division type and division method:

This section describes the definition, creation, and usage of the division type and division method. Here we will mainly introduce the features of the division method and division type. The Division type mainly connects the designer and other code generators. With the division type model, the code generator can have a free operation file, or it can rewrite the entire file every time as long as it is willing.

Some code generators can also choose not to generate any C # file, but to generate it again when building. Code generators are widely used, such as Web server proxies and ORM tools to generate configuration files. The application in the orm tool is as follows:

The segment type is also widely used in other aspects. The segment type can help us reconstruct it. (The first step of refactoring is to divide a large number of types into smaller classes. Many associated content can be divided into segments stored on two or more files .)

The Division type can also help us to perform unit tests.

The division method can specify a behavior in a manually created file and use this behavior in an automatically generated file.

III. C # Segment-type instances:

The Division type and division method are a linguistic feature. Here we provide a project that uses the division type. Https://github.com/fiidau/Phasing-Utility

Iv. Summary:

There are still many introductions to the division type and division method. In this article, I just gave a brief introduction. The distribution type is also widely used and can greatly improve the quality of our code. I hope this article will help you.

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.