Home

Partial Class Implementation

     

Introduction

 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 other files. This is referred to as partial implementation.

 
Author Note

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 had created a partial class, or you got a partial class from somebody (not as part of a DLL nor from another type of library), and you find out that the class is not complete, you can then complement it. One of the rules you must observe is that the partial class must have been marked as partial, as we did above. One of the advantages of partial implementation is that you don't have to get back to the first or previous file to modify it in order to complement the class. You can simply start another file and continue the class in it. Two other rules you must observe are that you must use the same name for the class and you must precede the class keyword with partial. Here is an example:

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 . . .

Once a partial class has been created, you can create another class based on it. The child class doesn't have to be partial, although it can be.

ApplicationApplication: Introducing Partial Implementation

  1. Start Microsoft Visual Studio
  2. To create a new project, on the main menu, click File -> New Project
  3. In the middle list, click Empty Project
  4. Set the Name to Geometry2 and click OK
  5. To add a new file, on the main menu, click Project -> Add New Item...
  6. In the middle list, click Code File
  7. Set the Name to Circle1 and click Add
  8. 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;
            }
        }
    }
  9. To make the class partial, add the partial keyword as follows:
    public partial class Circle
    {
    
        . . . No Change
            
    }
  10. To create a new file, in the Solution Explorer, right-click Geometry2 -> Add -> New Item...
  11. In the middle list, make sure Code File is selected; otherwise, click it.
    Set the Name to Circle2 and click Add
  12. 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);
        }
    }
  13. To create a new file, in the Solution Explorer, right-click Geometry2 -> Add -> New Item...
  14. In the middle list, make sure Code File is selected; otherwise, click it.
    Set the Name to Geometry and click Add
  15. 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;
        }
    }
  16. To execute the application, on the Standard toolbar, click the Start Debugging button Start Debugging
  17. Enter the radius as 10.08 and press Enter
    ==============================
    Circle Characteristics
    ------------------------------
    Radius:        10.08
    Diameter:      20.16
    Circumference: 63.3344544
    Area:          319.205650176
    ==============================
    
  18. Press Enter to close the DOS window and return to your programming environment
  19. On the main menu, click File -> Close Solution (Microsoft Visual Studio) or File -> Close Project (Microsoft Visual C# 2010 Express)
  20. When asked whether you want to save, click Discard
 
 

Home Copyright © 2010-2016, FunctionX