Tuesday, March 16, 2010

Partial Classes

Partial classes were added to C# 2005 to facilitate breaking a type (class, struct, or interface) into more than one section and each in a separate file. This is useful when writing large projects or using machine-generated code. This feature also helps developer teams to collaborate on the dame application.
To declare a partial class, all sections of the class must use the modifier partial right before the word class. Other modifiers can precede the key –word partial. The following example declares a class called TimeSheet divided into two files: -
//File1.cs
public partial class TimeSheet
{
    public void AddWorkingHours()
    {
        //…
    }
    public void CalcukateSalary()
    {
        //…
    }
}
//File2.cs
public partial class TimeSheet
{
    public void SubtractVacationTime()
    {
        //…
    }
}
On compilation, all sections of the class are put together to create the class. All the sections of course must be included in the same namespace, but cannot span assemblies.
The following rules and restrictions control the use of partial classes:
1)    All partial classes that constitute one class must use the same accessibility level.
2)    If one part of the class is declared using the modifiers abstract or sealed, the modifier will apply to the whole class.
3)    If one partial class is derived from a base class, all the other partial classes will inherit this base class even if is not mentioned in their declarations. This means that if we have two partial classes, as in this example:
partial class Employee: Person
{
    //…
}
partial class Employee: Citizen
{
    //…
}
The class Employee would effectively be:
class Employee: Person,  Citizen
{
    //…
}
4)    What applies to inheritance from classes applies to implementing interfaces.
The following example consists of two files – file1.cs and file2.cs – that contain a definition of the Employee class. Each file contains part of the class declared as partial. To compile the program, use the following command:
Csc/out:Ex5-14.exe file1.cs file2.cs
This combines the two files, file1.cs and file2.cs, and generates an executable file named Ex5-14.exe.
Example: -
//Example 5-14.cs
//file1.cs
public partial class Employee
{
    private string name;
    private string id;
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    public string Id
    {
        get { return id; }
        set { id = value; }
    }
}
file2.cs
//Example 5-14.cs
//file2.cs
using System;
public partial class Employee
{
    public void DisplayInfo()
    {
        Console.WriteLine(“Employee’s name: {0}”, name);
        Console.WriteLine(“Employee’s id: {0}”, id);
    }
}
class MyClass
{
    static void Main(string[] args)
    {
        //Crate object:
        Employee emp = new Employee();
        //Read name and id:
        Console.Write(“Please enter the employee’s name: “);
        emp.Name = Console.ReadLine();
        Console.Write(“Please enter the employee’s id: “);
        emp.Id = Console.ReadLine();
        //Display information:
        emp.DisplayInfo();
    }
}

No comments:

Post a Comment