Partial Class Implementation
In C#, you can create a class (the same class) in different files. This means that you can start a class in one file and continue it in another file or in…
Introduction#
C# allows you to split the definition of a single class across multiple separate files.
If you have programmed in C++ or C++/CLI, don't confuse its header and source files with C#'s partial implementation of classes. In C++ or C++/CLI, you can include the structure of a class with its member variables (called fields in C#) and the declaration of its member functions (called methods in C#). In C++, a header file has the extension .h. Here is an example of a C++/CLI header file: Header File: Cylinder.h
#pragma once
using namespace System;
public ref class CCylinder
{
private:
double rad;
double hgt;
public:
CCylinder(void);
CCylinder(double radius, double height);
property double Radius
{
double get() { return rad; }
void set(double value) { rad = value; }
}
property double Height
{
double get() { return hgt; }
void set(double value) { hgt = value; }
}
double Volume();
};In C++, after creating a header file, you can create its associated source file. The source file has the extention .cpp. Here is an example of the source file corresponding to the above header file: Source File: Cylinder.cpp
#include "Cylinder.h"
CCylinder::CCylinder(void)
: rad(0.00), hgt(0.00)
{
}
CCylinder::CCylinder(double radius, double height)
: rad(radius), hgt(height)
{
}
double CCylinder::Volume()
{
return rad * rad * hgt * Math::PI;
}The above class can be tested with the following: Source File: Exercise.cpp
#include "Cylinder.h"
using namespace System;
CCylinder ^ Initialize()
{
CCylinder ^ c = gcnew CCylinder(36.12, 18.84);
return c;
}
const void Show(CCylinder ^ vol)
{
Console::WriteLine(L"Radius: {0}", vol->Radius);
Console::WriteLine(L"Height: {0}", vol->Height);
Console::WriteLine(L"Volume: {0}", vol->Volume());
}
int main()
{
CCylinder ^ cyl = Initialize();
Show(cyl);
return 0;
}As we have seen so far, in C#, you cannot simply and only declare a method in a file for a forward (later) implementation, as it's done in C, C++, C++/CLI, Ada, and (Object) Pascal. In C#, to create a class in various files, start the class in one file but precede the class keyword with partial. Here is an example of a file named first.cs that contains some (2) private fields and some (2) properties: Source File: geometry1.cs
using System;
partial class Cylinder
{
private double rad;
private double hgt;
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; }
}
}After creating the class in one file, you can use it like any of the classes as we have done so far. Here is an example: Source File: Exercise.cs
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;
}
}This would produce:
Radius: 36.12
Height: 18.84
Press any key to continue . . .If you are working with a partial class—whether you created it or received it—you can extend its functionality. Source File: geometry2.cs
using System;
partial class Cylinder
{
public Cylinder()
{
this.rad = 0.00;
this.hgt = 0.00;
}
public double Volume()
{
return rad * rad * hgt * Math.PI;
}
}This class can then be tested as follows: Source File: Exercise.cs
using System;
class Program
{
static Cylinder Initialize()
{
Cylinder c = new Cylinder();
c.Radius = 42.66;
c.Height = 26.48;
return c;
}
static void Show(Cylinder vol)
{
Console.WriteLine("Radius: {0}", vol.Radius);
Console.WriteLine("Height: {0}", vol.Height);
Console.WriteLine("Volume: {0}", vol.Volume());
}
static int Main()
{
Cylinder cyl = Initialize();
Show(cyl);
Console.WriteLine();
return 0;
}
}This would produce:
Radius: 42.66
Height: 26.48
Volume: 151394.310951986
Press any key to continue . . .After a partial class is defined, it can serve as a base for other classes to inherit from. Application: Introducing Partial Implementation
- Start Microsoft Visual Studio
- To create a new project, on the main menu, click File -> New Project
- In the middle list, click Empty Project
- Set the Name to Geometry2 and click OK
- To add a new file, on the main menu, click Project -> Add New Item...
- In the middle list, click Code File
- Set the Name to Circle1 and click Add
- Change the file as follows: public class Circle { private double _radius; public double Radius { get { return (this._radius <= 0) ? 0.00 : this._radius; } set { this._radius = value; } } public double Diameter { get { return this.Radius * 2; } } public double Circumference { get { return Diameter * 3.14159; } } public double Area { get { return this.Radius * this.Radius * 3.14159; } } }
- To make the class partial, add the partial keyword as follows: public partial class Circle { . . . No Change }
- To create a new file, in the Solution Explorer, right-click Geometry2 -> Add -> New Item...
- In the middle list, make sure Code File is selected; otherwise, click it. Set the Name to Circle2 and click Add
- Change the file as follows: using System; public partial class Circle { public Circle(double radius = 0.00) { this.Radius = radius; } public void Present() { Console.WriteLine("Radius: {0}", this.Radius); Console.WriteLine("Diameter: {0}", this.Diameter); Console.WriteLine("Circumference: {0}", this.Circumference); Console.WriteLine("Area: {0}", this.Area); } }
- To create a new file, in the Solution Explorer, right-click Geometry2 -> Add -> New Item...
- In the middle list, make sure Code File is selected; otherwise, click it. Set the Name to Geometry and click Add
- To test the class, type the following: using System; public class Program { private static Circle Initialize() { Console.Write("Enter the radius: "); double rad = double.Parse(Console.ReadLine()); Circle c = new Circle(rad); return c; } public static int Main() { Console.Title = "Geometric Shapes"; Console.WriteLine("This application allows you to process a circle"); Circle circ = Initialize(); Console.Clear(); Console.WriteLine("\n=============================="); Console.WriteLine("Circle Characteristics"); Console.WriteLine("------------------------------"); circ.Present(); Console.WriteLine("==============================\n"); Console.ReadKey(); return 0; } }
- To execute the application, on the Standard toolbar, click the Start Debugging button
- Enter the radius as 10.08 and press Enter ============================== Circle Characteristics ------------------------------ Radius: 10.08 Diameter: 20.16 Circumference: 63.3344544 Area: 319.205650176 ==============================
- Press Enter to close the DOS window and return to your programming environment
- On the main menu, click File -> Close Solution (Microsoft Visual Studio) or File -> Close Project (Microsoft Visual C# 2010 Express)
- When asked whether you want to save, click Discard
Home Copyright © 2010-2011 C# Key

