views:

138

answers:

2
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?

+16  A: 

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.

Michael Petrotta
The OP should also note that methods may be partial as well.
Bloodyaugust
+2  A: 

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.

mumtaz