- Can somebody tell why and for what it is declared as partial?
- what is its use?
- Is this possible to declare it without partial?
views:
63answers:
3It'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"?
The form design has its code in a different .cs file and its events and other code (that you code in) is stored in a different .cs file. But both the file is declared using the same class name, i.e. the name of your form. Thats why this is marked as "partial". This holds also true for ASP.NET pages, if you use code-behind.