This is a mirror of official site: http://jasper-net.blogspot.com/

Introducing C# 2.0 Partial Types

| Sunday, March 6, 2011
Introduction

Examine the example below:

//definition for BinaryTree.h
class BinaryTree
{
public:
      BinaryTree(void);
      ~BinaryTree(void);
      int seed;
      char* name;
      void Load(char* name);
      char* Find(); 
};

In C++, class definitions can be separated from the implementations. The above snippet is taken from a file BinaryTree.h which contains all the definitions associated with the BinaryTree class but no implementation code. Continuing with the example, the implementation file BinaryTree.cpp would be defined as follows:

#include ".\binarytree.h"

BinaryTree::BinaryTree(void)
{
}

BinaryTree::~BinaryTree(void)
{
}

void BinaryTree::Load(char* str){
      this->name = str;
}

char* BinaryTree::Find(){
      return this->name;
}

As you can see from the sample, the first line of BinaryTree.cpp explicitly declares the inclusion of the class definition found in binarytree.h. Consequently, this completely separate file can use members defined in the latter as though they were defined there. The sample is, of course, nonsensical, but the concept nevertheless prevails; the ability to create types across multiple files.

It is important to note that the idea of having multiple public types in one file was so revolting to the devout object oriented programmer at one time that entire languages, Java for instance, excluded it. However, you will find that while it is good programming practice to maintain all source code for a type in a single file, sometimes a type becomes large enough that this is an impractical constraint.

Partials

C# 2.0 introduces the concept of partial type definitions for classes, structs, and interfaces, as a means to break types in a program into multiple pieces to be stored in different source files for easier maintenance and development. This approach is particularly useful when the need arises to use nested types or provide interface implementation.

Unlike C/C++, where a #include keyword was used to explicitly bind the contents of a header file to the implementation file, C# 2.0 partial type links are inferred through the type names given. Also, these are no interface definitions necessary in C# 2.0. A sample of a partial type is given below:.

//Binarytree_header.cs
public partial class BinaryTree
{
    int seed;
    string name;
    public BinaryTree()
    {
    }
}

The above example attempts to recreate the BinaryTree sample we saw earlier in the chapter, using C#; notice that no include statements are needed, nor are any function prototypes defined. Further implementation of this class may now be defined in other files; for instance, a BinaryTree_Load.cs file may be created which produces more BinaryTree functionality.

//BinaryTree_load.cs: Load functionality
public partial class BinaryTree
{
    public void Load(string name)
    {
          this.name = name;
    }
}

We can also create another file BinaryTree_find.cs which would have all the functionality associated with finding an element in the tree.

//BinaryTree_find.cs: find functionality
public partial class BinaryTree
{
    public string Find()
    {
          return name;
    }
}

As you can see from the above samples, the new type modifier, partial, is used when defining a type in multiple parts. It indicates that additional parts may exist elsewhere. I stress the word may because each partial type declaration is autonomous. The example below illustrates this by creating a partial type declaration that includes no other parts:

public partial class BinaryTree
{
    int x;
}

The code above will compile successfully. Adding another part to the type will not change this successful compilation.

public partial class BinaryTree
{
    int y;
}

Finally, a constructor can be added to instantiate the two variables defined in both parts of the type. A sample showing all three parts is highlighted below:

public partial class BinaryTree
{
    int x;
}

public partial class BinaryTree
{
    int y;
}

public partial class BinaryTree
{
    public BinaryTree()
    {
          this.x = 100;
          this.y = 100;
    }
}

Read more: Codeproject

Posted via email from Jasper-net

0 comments: