C# Language

Class Partial Implementation

In all of the classes we have defined so far, we were using a single file to implement the class. In C#, you can create a class (the same class) in…

1 min read295 words9 code samples

Up to this point, every class we defined was contained within a single source file.

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 methods. In C++, a header file has the extension .h. Here is an example of a C++/CLI header file: Header File: Cylinder.h

C#
#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

C#

#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

C#
#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, and (Object) Pascal. In C#, to create a class in various classes, 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

C#
using System;

partial class Cylinder
{
    private double rad;
    private double hgt;

    public Cylinder(double radius, double height)
    {
        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 as any of the classes as we have done so far. Here is an example: Source File: Exercise.cs

C#
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:

C#
Radius: 36.12
Height: 18.84

Press any key to continue . . .

When using a partial class, you can combine parts from different files into one cohesive unit. Source File: geometry2.cs

C#
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

C#
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:

C#
Radius: 42.66
Height: 26.48
Volume: 151394.310951986

Press any key to continue . . .

A class can be built upon a previously created partial class through standard inheritance. Home Copyright © 2008 C# Key