0

In .Net when an application is compiled, it groups all the partial classes and considers them as a single object.

public partial class DemoClass
{
        public void Hello()
        {
        }
}

public partial class DemoClass
{
        public void Bye()
        {
        }
} 

In code Behind Partial Class is there always

 public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }

Then why do we have partial classes in .NET?

lil
  • 336
  • 7
  • 19
  • In some cases, auto generated files are generated as partial so they have some extensibility. This can be very convenient. For example: Entity framework classes are partial so you can create enum properties that wrap integer fields. – Silvermind Jul 07 '15 at 05:50
  • This may be the reason JavaScript doesn't support it. – lil Jul 07 '15 at 06:09

6 Answers6

3

Partial classes provide a way to split the code of a class across files.

This way one file can contain generated code and another file could contain handwritten code. The generated code can than be regenerated without messing with the handwritten code. Had the custom code been added to the same file as the generated code, the regeneration would have overwritten it.

Visual Studio itself uses this in many places where designers generate code and users add logic. Sometimes Visual studio hides the generated code by only generating it during compilation (Compiling WPF applications generates a 'bridge' class between Xaml and 'code behind') and sometimes generated code is put in partial classes without providing an empty twin class which is left to the developer.

In general: when generating code use partial classes as a service to the developers that use the generator. And while we are at it: consider providing partial methods as well to allow the developer to hook in to the logic of the generated code so the developer is free to chose between implementing the partial method, sub classing the generated class for the correct reasons.

Emond
  • 50,210
  • 11
  • 84
  • 115
2

Partial is for splitting code accross multiple files. There are 3 reasons to have partial classes:

  • For necessary seperation when creating designers and it should avoid the mixing of user and designer code. For example: Windows Forms classes
  • For splitting large classes, however this should be avoided anyway
  • For nested types ( This is something i personal do regulary )
Felix K.
  • 6,201
  • 2
  • 38
  • 71
1

Partial classes are useful when some classes are generated with t4 templates like the Models with EntityFramework and an .edmx.

So you can extend your models with another partial class without be bothered by the automatic generation.

When is it appropriate to use C# partial classes?

Community
  • 1
  • 1
Jean F.
  • 1,775
  • 1
  • 19
  • 16
1

Partial is used when you have generated code, code which is prone to change, but its true source is for example an .edmx file. So each time you change the .edmx file, you regenerate the code. That means that every change you make in that file gets lost. Now, you want to extend that code, what do you do? Either inherit from the class you want to extend, or you use partial. Remember, when you inherit a lot more stuff happens. When you use partial it just becomes part of the same class.

Also, sometimes you can't inherit, because the base class is sealed, and you also might run into issues with badly written Reflection code.

Anemoia
  • 7,928
  • 7
  • 46
  • 71
1

Well there are many ways programmers use these partial classes, such as:

  • To divide one class into many smaller files (if the file gets way too large and you have bad scroll on your mouse)
  • Allow extension of the base class
  • Some machine generated code is marked as partial so you can add your own logic without seeing generated code.
  • etc

I think that all of these reasons are bad. I try to avoid partial classes. In fact I mark most of my classes with sealed. I belive that the code would be easier to read if we wouldn't use partial classes

nomail
  • 635
  • 6
  • 21
  • What about auto generated classes? – Jean F. Jul 07 '15 at 05:56
  • Yeah. It's 3rd in a list – nomail Jul 07 '15 at 05:57
  • I mean why do you think this reason is bad? – Jean F. Jul 07 '15 at 05:58
  • Oh. sorry. Well I wouldn't rely on something that might be regenerated later. I would make a converter from that class to my own representation that will never change by itself thus having ruining the logic of application. Also this way I separate my code from not-my-code. It makes code more transparent – nomail Jul 07 '15 at 06:02
1

Sometimes in different libraries you can see implementation of standard business logic but with approach to extend using partial classes.

For example in the EntityFramework you can generate database Models using Database first approach then you will have model classes - partial classes. They used partial classes to separate generated code from your own extended code.

Here is a informative article about why it's present in C# and asp.net http://www.codeproject.com/Articles/313352/real-time-use-of-Partial-classes-and-partial-met

Andrii Tsok
  • 1,386
  • 1
  • 13
  • 26