views:

63

answers:

3
  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?
+2  A: 

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

Martin Peck
Is this possible to declare it without partial?Because I have seen some examples in net which are not using the partial keyword.Or does the older version of Visual Studio support the non partial form?
Babu
They're almost certainly from previous versions of VS.NET.
Martin Peck
Thanks you Peck.
Babu
A: 

It's declared partial because the class is defined in two files. Whenever you want to split up a class's definition into several files, you can use the keyword partial.

Oskar
A: 

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.

Bhaskar
Thanks for your info.
Babu