Partial classes)

Source: Internet
Author: User

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.

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.