What does the partial mean?

痴心易碎 提交于 2019-12-04 18:11:04

问题


public partial class Form1 : Form

What does the partial in this declaration mean? I understand we have a class Form1 that inherits from Form. But what does the partial mean?


回答1:


It allows you to split the definition of your class into two or more separate files.

See this MSDN article, "Partial Class Definitions" for more information:

It is possible to split the definition of a class or a struct, or an interface over two or more source files. Each source file contains a section of the class definition, and all parts are combined when the application is compiled. There are several situations when splitting a class definition is desirable:

  • When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously.
  • When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when creating Windows Forms, Web Service wrapper code, and so on. You can create code that uses these classes without having to edit the file created by Visual Studio.

You're likely referring to the default behavior of Visual Studio when creating forms. This allows the definition of the form to be split between the file that you own and can modify, and the file that Visual Studio owns (Form1.Designer.cs). This avoids a lot of headaches we saw with Visual Studio 2002 and 2003, when the developer and the IDE would step over each other's toes all the time with their edits.




回答2:


This permits partial class definitions in source files which are combined into a single class at compilation. In your case one half of the class code is autogenerated by the code generator and hidden from you in a file with partial class declaration (there will be a lot of code in there). You are given a clean slate with partial class definition to allow you to enter code in this partial definition so at compilation the autogenerated code and your code are combined into a single class derived from the class Form. Its a commone way of of combining the machine's and the man's code in visual designers etc to allow you to not write the plumbing boring code and just concentrate on what you will like to do.



来源:https://stackoverflow.com/questions/3861171/what-does-the-partial-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!