Let's take WinForms as the example here.
Visual Studio has a GUI editor for making Forms. You drag and drop things from a Toolbox onto a grid. Ya' make buttons, labels, other things, etc. Pretty swanky.
Now, behind the scenes, Visual Studio needs to take that GUI information and turn it into code. It generates a code file (such as Form1.Designer.cs
), which is the code needed to make the form you created visually.
This code file is a class. It has the partial
keyword. That's the key here. That's what the bullet point is talking about.
Let's say the class didn't have a partial
keyword. How you would add your own custom logic to the form? Uh, well, you couldn't. You could inherit the class with your own, but that's not the same thing. You'd have to manually modify the generated code file, which, guess what, would be overwritten each time you make a change in GUI form designer. That's not very swanky.
So, that's why you have a Form1.cs
and Form1.Designer.cs
file. The second is the generated file with the partial
keyword, and the first is where you can extend the file with your own custom logic.
One file for your stuff, one file for VS's stuff. You can write your custom logic in one file, and not have to worry that the changes will be removed when Visual Studio regenerates its file when the form layout is modified. They are merged at compile time because the partial
keyword indicates they should be treated as one big class, just from two (or more) files.