C # 2.0 can split the definition of a class, struct, or interface into two or more source files, adding the partial keyword before the class declaration.
Code:
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks;namespace Test{ class Program{ static void main (String[] args) { a a = new a ();}} partial class a{ public a () {Printhello (); console.read ();} partial void printhello () { console.writeline ("Hello");}} partial class a{ partial void printhello (); }}
Results:
Hello
Under what circumstances are partial classes used?
– When working with large projects, having a class spread across separate files allows multiple programmers to process the class at the same time (equivalent to supporting parallel processing, which is useful);
– When you use an automatically generated source, you can add code to the class without recreating the source files. Visual Studio uses this method when creating Windows Forms, Web forms. You can create code that uses these classes without editing the files created by Visual Studio. In other words: The system automatically creates a file (typically a property of a form and a control on a form), and another or several files record the code that the user has written. These two parts can make the structure appear very clear, the user only need to focus on the part of their own responsibility (if necessary, these two parts can be called each other). When the editor is running, the system automatically synthesizes the two parts into one file.
There are a few things to note about using the partial:
1. Use the partial keyword to indicate that other parts of the class, struct, or interface can be defined within a namespace
2. All parts must use the partial keyword
3. Each part must have the same accessibility, such as public, private, etc.
4. If any part is declared abstract, the entire type is treated as an abstract
5. If any part is declared as sealed, the entire type is considered sealed
6. If any part of the declaration inherits the base class, the entire type inherits the class
7. Each section can specify a different base interface, and the final type will implement all the interfaces listed by all the partial declarations
8. Any class, struct, or interface member declared in a partial definition can be used by all other parts
9. Nested types can be partial, even if the type in which they are nested is not part of the division itself. As shown below:
Class Container
{
Partial class Nested
{
void Test1 ();
}
Partial class Nested
{
void Test2 ();
}
}
Some limitations of using distribution classes:
1. All partial type definitions that are to be part of the same type must be decorated with partial. As shown below:
Public partial class A {}
public class A {}//Error, must also is marked partial
2. The partial modifier can only appear in the position immediately preceding the keyword class, struct, or interface (no enumeration or other type can use partial);
3. All partial type definitions that are to be parts of the same type must be defined in the same assembly and in the same module (. exe or. dll file). A partial definition cannot span multiple modules;
4. Class names and generic type parameters must match in all partial type definitions. A generic type can be partial. Each partial declaration must use the same parameter name in the same order.
Reference:
Http://blog.sina.com.cn/s/blog_4aa25f130100esoe.html
Http://www.jb51.net/article/63131.htm
This article comes from the "Ricky's blog" blog, please be sure to keep this source http://57388.blog.51cto.com/47388/1656053
C #-Partial methods and partial classes (partial)