There was an interview when the interviewer asked me, do you know some of the classes?
I say that classes that are decorated with the partial keyword are part of the class
He asked, do you know why there are some classes?
I froze, this question I did not think about, and usually when not how to use a part of the class, I can not answer.
Now that I have a look at the information, let's share some of the classes I know.
Partial classes are classes declared with partial, which allow us to split a class into two or more classes, and when the application compiles, multiple partial classes are combined into a class, and the partial keyword can be used to declare structs and interfaces.
So here's the question, why do you have some classes?
Since Microsoft has created such a thing, there must be a sense of its existence.
One of the meanings of existence:
If you notice, you will find the WebForm program or the Windowform program we created. By default, two files are generated automatically: Webfrom1.aspx.cs and Webform1.aspx.designer.cs, and then you'll find that all two classes are declared with the keyword partial, yes, that's where some of the applications are, and I think it's for Code-behind technology. Because of the declaration of some classes, the system automatically generated code and our own code to separate, has been reading and understanding of the Code to improve development efficiency.
The second meaning of existence:
When dealing with large projects, a complex class can be divided into several parts of the class for multiple programmers to develop at the same time. However, Microsoft declares that this is a benefit, in addition to the example above, I have not seen elsewhere in the use of partial classes, the use of partial classes let a number of parts of the class work together ... Here are some of the things that some classes pay attention to when they are used: if a partial class is declared, and the class name of another class and a partial class is the same, the compilation is not passed, and two must be part of the class to compile through two: the access modifiers of the multiple partial classes are consistent. Three: If a part of the class is abstract or sealed class, then all the partial class is declared abstract or sealed class four: If the partial class is a derived class, then its base class to the same part of the method is declared partial in a partial class method when a method is declared as partial, there cannot be a method body, You can only declare the method, cannot implement it, or after declaring, write a method of the same name, then there is the method body, you can have the code to implement the method, or in this part of the class declaration method, in another part of the class declaration and implementation of it. The default modifier for some methods is private, so it is not possible to use private to modify it, and some methods cannot have a return value, that is, void only.
C # Partial classes and partial methods