-1

Microsoft's docs state some reasons why it's a good idea to use partial, one of which deals with generated code files.

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 it creates Windows Forms, Web service wrapper code, and so on. You can create code that uses these classes without having to modify the file created by Visual Studio.

I understand the general concept of partial, but why include the keyword in generated files?

gunr2171
  • 16,104
  • 25
  • 61
  • 88

2 Answers2

1

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.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
0

In C#, you can split the definition of a class into multiple partial definitions. They can be in separate files. Unlike in Java where each class is in a single file. This partial works in a single file also, so you can say:

public partial class Employee
{
    public void DoWork()
    {
    }
}

public partial class Employee
{
    public void GoToLunch()
    {
    }
}

I never do this as big classes are typically horrible, so have not had a need to split them into files, but the docs mention examples where they have reasons:

There are several situations when splitting a class definition is desirable:

  • When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time.
  • 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 it creates Windows Forms, Web service wrapper code, and so on. You can create code that uses these classes without having to modify the file created by Visual Studio.
  • When using source generators to generate additional functionality in a class.

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods

antont
  • 2,676
  • 1
  • 19
  • 21
  • 1
    It is not extending in the explicit/class hierarchy sense of the word, but it is as the above code example shows: you can have more than one definition of the same class, which is especially useful when working with generated code (as whatever changes made to the generated code will be erased when the code is generated again). – ryanwebjackson Oct 13 '21 at 01:58
  • Yep their wording could be better - 'uses these classes' could be maybe 'adds more definitions to these classes, in separate files' – antont Oct 13 '21 at 06:48