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.
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) A part of the code in a type is the code generated by the automation tool, and should not be mixed with the code we write.
(3) many people need to work together to compile a class.
The local type is compiled on the pure language layer without affecting any execution mechanism. In fact, the C # compiler will
Merge partial types into a complete class.
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 is included 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 on one part, and abstract cannot be used on another part.
Use sealed.
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 {}
Personal Website: http://www.mymainet.com
From: http://blog.csdn.net/fredlau/archive/2007/12/15/1940749.aspx