Partial class

from Wikipedia, the free encyclopedia

A partial class is a term from object-oriented programming and describes a procedure for dividing classes into several source code files or declaring them at different locations within a file.

reasons

  • Increased readability for very large classes.
  • Different developers can work on the same class without interfering with each other.
  • Separation of interface and class definition or also of public and private areas.
  • Separation of concerns , similar to aspect-oriented programming.( 1 )
  • Separation of automatically generated code from a code generator and code written manually by humans. The generated code can thus be updated more easily at any time without the two points interfering with each other.

Separation of Concerns

Partial classes also support separation of concerns . The following C # example shows a Bear class that has several aspects. These are implemented in several files.

Bear_Hunting.cs
public partial class Bear
{
   private IEdible Hunt()
   {
       // Gibt Nahrung zurück...
   }
}
Bear_Eating.cs
public partial class Bear
{
   private int Eat(IEdible food)
   {
       return food.Nutrition.Value;
   }
}
Bear_Hunger.cs
public partial class Bear
{
   private int hunger;

   public void MonitorHunger()
   {
        // An dieser Stelle beziehen wir uns auf Methoden, die in den anderen partiellen Klassen definiert sind
        if(hunger > 50)
            hunger -= this.Eat(this.Hunt());
   }
}

By using partial classes, it is very easy to extend program features by adding source code files by compiling additional files. For example, these can be features that are offered to a customer at an additional cost.