Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the form class in windows form application is declared as partial?

  1. Can somebody tell why and for what it is declared as partial?
  2. what is its use?
  3. Is this possible to declare it without partial?
like image 884
Babu Avatar asked Dec 29 '22 10:12

Babu


1 Answers

It's declared partial because VS.NET will put "designer" code into a seperate .cs file that is actually, at compile time, part of the same class.

If you have a Form1.cs you should also have a Form1.Designer.cs on the file system as well. When you drop controls onto a form VS.NET will add code into that designer file. It's seperate because you don't want to see or touch that code, and don't want it getting in the way of your own code.

So, the partial class feature in C# is mainly there to allow the seperation of your code and designer code. At compile time the two (or more) files making up the partial class become a single class - at runtime there isn't any difference between a class defined as partial and one that isn't.

"Back in the day" before partial existed all the designer code was lumped in with your own, and this made for messy .cs files and the possibility of developers changing code that VS.NET was expecting to have exclusive rights to. So, a Form will work fine (if written without designer support) without being partial (e.g. if you were generating the entire file yourself from a T4 template).

Why would you want to remove the "partial"?

like image 119
Martin Peck Avatar answered Feb 07 '23 23:02

Martin Peck