Tuesday, June 21, 2011

Partial Class

A partial class, or partial type, is a feature of some OOPS in which the declaration of a class may be split across multiple source-code files, or multiple places within a single file.

Purpose
  • Very large classes
  • Allowing multiple developers to work on a single class at the same time without the need for later merging files in source control.
  • Allowing a separation between the class interface and the implementation-related definitions (Separate definitions of the public and private parts)
Implementation
The implementation of partial classes is quite straight-forward and architecture-transparent. When compiling, the compiler performs a phase of precompilation first it "unifies" all the parts of the partial class into one logical class, and from that point, normal compilation takes place.

Example
using System;
partial class Cylinder
{     
private double hgt;
private double rad;  
public Cylinder(double radius = 0D,  
               double height = 0D)    
 {         
this.rad = radius;         
this.hgt = height;     
}      
public double Radius     
{         
get 
{ 
return rad; 
}      
set 
{ 
rad = value; 
}     
}      
public double Height     
{      
get { return hgt; }      
set { hgt = value; }     
} }
Example1
using System; 
class Program 
{     
static Cylinder Initialize()     
{         
Cylinder c = new Cylinder(36.12, 18.84);         
return c;     
}      
static void Show(Cylinder vol)     
{         
Console.WriteLine("Radius: {0}", vol.Radius);         
Console.WriteLine("Height: {0}", vol.Height);     
}         
static int Main()    
{         
Cylinder cyl = Initialize();          
Show(cyl);          
Console.WriteLine();         
return 0;     
} }


No comments:

Post a Comment