C #: details about partial on the code page on the code separation page!

Source: Internet
Author: User
C #2.0-partial
The following documents are referenced in Mr. Li Jianzhong's speech.

1. What is a local type?

C #2.0 introduces the concept of local types. The local type allows us to divide a class, structure, or interface into several parts, which are implemented in several different. CS files.

The local type applies to the following situations:

(1) The type is very large and should not be implemented in a file.
(2) Part of a TypeCodeThe code generated for automated tools should not be mixed with the code we have compiled.
(3) many people need to work together to compile a class.

Local types are compiled on the language-only layer without affecting any execution mechanism. In fact, the C # compiler still combines the local types of each part into a complete class during compilation.

Public partial class Program
{
Static void main (string [] ARGs)
{
}
}

Partial class Program
{
Public void test ()
{
}
}

2. Restrictions on local types

(1) local types are only applicable to classes, interfaces, and structures. Delegation and enumeration are not supported.
(2) Each part of the same type must have a modifier partial.
(3) When using a local type, each part of a type must be in the same namespace.
(4) Each part of a type must be compiled at the same time.

3. Notes for local types

(1) the keyword "partial" is a context keyword. It indicates a keyword only when it is put together with class, struct, and interface. Therefore, the introduction of partial does not affect the variables named partial in the existing code.
(2) Each part of the local type is usually put in several different. CS files separately, but the C # compiler allows us to put them in the same file.

4. Local application features

The features of local types have the "accumulate" effect.

[Attribute1, attribute2 ("hello")]
Partial class class1 {}

[Attribute3, attribute2 ("exit")]
Partial class class1 {}

Equivalent

[Attribute1, attribute2 ("hello"), attribute3, attribute2 ("exit")]
Class class1 {}

Note: The attribute2 attribute can be used multiple times on the class.

5. Modifier on local type

(1) The access modifiers on each part of a type must be consistent.
(2) If a part of a type uses the abstract modifier, the entire class will be treated as an abstract class.
(3) If a part of a type uses the sealed modifier, the entire class will be considered as a sealed class.
(4) Each part of a class cannot use conflicting modifiers. For example, abstract cannot be used in one part, and sealed can be used in another part.

6. Local base classes and interfaces

(1) The base classes specified on each part of a type must be consistent. A certain part may not specify the base class, but if it is specified, it must be the same.
(2) interfaces of local types have the "accumulate" effect.

Partial class class2: iinterface1, iinterface2 {}
Partial class class2: iinterface3 {}
Partial class class2: iinterface2 {}

Equivalent

Class class2: iinterface1, iinterface2, iinterface3 {}

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.