Partial class (partial classification) in C ),

Source: Internet
Author: User

Partial class (partial classification) in C ),

C #2.0 splits the definition of a class, structure, or interface into two or more source files, and adds the partial keyword before the class declaration.

For example, the following PartialTest class

Class PartialTest

{

String Str_FieldTest;

Int Int_FieldTest;

Public void DoTest ()

{

Debug. Print ("Test ");

}

}

You can write the following form in different source files:

Write in a file:

Partial class PartialTest

{

String Str_FieldTest;

Int Int_FieldTest;

}

Write in another file:

Partial class PartialTest

{

Public void DoTest ()

{

Debug. Print ("Test ");

}

}

 

Under what circumstances do I use a division class?

-When processing large projects, assigning a class to multiple independent files allows multiple programmers to process the class at the same time (equivalent to supporting parallel processing and practical );

-When an automatically generated source is used, the code can be added to the class without re-creating the source file. Visual Studio uses this method when creating Windows and Web forms. You can create code using these classes without editing the files created by Visual Studio. In other words, the system will automatically create a file (usually recording the properties of controls in the form and form), and the other or several files will record the Code Compiled by the user. The two parts can be separated to make the structure very clear. Users only need to pay attention to the part they are responsible for (if needed, the two parts can be called each other ). When the editing is run, the system will automatically combine the two parts into a file.

 

Note the following when using Partial:

1. Use the partial keyword to indicate that other parts of the class, structure, or interface can be defined in the namespace

2. All parts must use the partial keyword

3. Each part must have the same accessibility, such as public and private.

4. If any part is declared as abstract, the entire type is considered Abstract

5. If any part is declared as sealed, the entire type will be regarded as sealed

6. If any part declares to inherit the base class, the entire type will inherit the class

7. You can specify different basic interfaces for each part. The final type will implement all interfaces listed in all segment declarations.

8. Any class, structure, or interface member declared in a segment definition can be used by all other parts.

9. nested types can be segments, even if the types they are nested in are not segments themselves. As follows:

Class Container

{

Partial class Nested

{

Void Test1 ();

}

Partial class Nested

{

Void Test2 ();

}

}

 

Restrictions on the Use of distribution classes:

 

1. All part type definitions of each part of the same type must be modified using partial. As follows:

Public partial class {}

Public class A {}// Error, must also be marked partial

2. The partial modifier can only appear next to the class, struct, or interface keyword (partial cannot be used for enumeration or other types );

3. to define all the parts of the same project in the same program set and in the same example (.exe or. dll file. The Division definition cannot span multiple modules;

4. The class name and generic type parameters must be matched in all segment type definitions. Generic types can be segments. Each division declaration must use the same parameter name in the same order.
Certificate ------------------------------------------------------------------------------------------------------------------------------------------

New Idea of partial class Extension function

The principle of opening and closing: "Closing modification and opening expansion ". In an object-oriented system, class inheritance is used for extension .. The partial class provided in. net provides new ideas for extended classes.

I. Application scenarios

There are many scenarios where partial class can be used. Here is an ORM example.
There is a Cat class in the system, and the property ID, Age, and Weight must be stored in the database, which is a common requirement in an information system. By reading the database structure, you can use tools to generate Cat class code. In addition, The ORM framework supports generating Cat objects from database information.
Currently, Cat does not have any actions. The customer said that we need a Miaow () function. In this case, you need to extend the Cat class generated by the ORM.
Certainly, we cannot modify the automatically generated code because it involves synchronization between the database structure and code. There are two ways to solve this problem: inheritance extension and partial class extension.

 

 

Ii. Inheritance Extension

The tool automatically generates a CatBase class, which has only attributes and is embedded in the ORM framework. Since the function needs to be extended, it is easy to think of inheritance to this base class, so there is a Cat class. The Cat class has the Miaow () function.
In the past, CatBase instances were used in the system. Now, to create a CatBase instance, you need to create a Cat instance. Let the ORM framework solve this problem.
The customer's requirements have been met, and our own code generation has not been damaged and the task has been completed.

Iii. partial class Extension

Partial class can write the code of a class to two or more code files. The compiler combines these files for compilation. A cool technology.
The Cat class generated by the tool remains unchanged. To add a function, you can create a code file and write the Miaow () function. All you need to do is to change the class declaration from class to partial class and complete the task.

Iv. Comparative Analysis

Both methods can meet the requirements. You need to carefully analyze the advantages and disadvantages.
Instance creation: partial class is more concise.
System Complexity: for the system, there is only one class in the partial class mode, and the Inheritance Method has two classes.
Inheritance logic: logically, Cat does not need a base class CatBase. This is only because of a limitation in the Code build process.
Maintainability: There are two code files in both methods, and there is no difference in maintenance costs.
Readability: Two Cat files are confusing.

In general, the use of partial class is more elegant.
This method of "inheritance" is more in line with the traditional thinking habits, and whether the partial class meets the open and closed principles is really hard to say. But in terms of software construction, I am a practical, and I can use either method or method.
In the orm scenario, partial class is better, but sometimes there is an inheritance relationship between the two classes, so inheritance must be used. In most cases, the inheritance method is required. However, with the partial class technology, we also need to consider this idea during design.

In VS2005, Form and DataSet all use the partial class method. The principle is the same as this. However, to promote this principle to "business entities", we may need to make some breakthroughs in understanding it.

Using partial class can indeed cause readability loss. Especially when a class is distributed in many files, it is best to have a specification to ensure the file naming.

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.