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 to [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 to class class2: iinterface1, iinterface2, iinterface3 {}