Monday, June 11, 2012

Partial Classes and Methods

Partial Classes and Methods

C# 2.0 introduced partial types to allow class definitions to be split between several files. C# 3.0 extends the concept with partial methods, allowing method signatures to be declared but not implemented. This is ideal for use with code generation tools.
In this post , I will try to explain the basic rules of partial classes and methods and how simple it is to use partial methods.


Partial Class

It is possible to split the definition of a class or a struct, an interface or a method over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled.

To split a class definition, use the partial keyword modifier, as shown here:
public partial class A
{
    public void Method1()
    {
    }
}

public partial class A
{
    public void Method2()
    {
    }
}

The partial keyword indicates that other part of class, struct or interface can be defined in namespace.All the parts must use partial keyword.All the parts of partial classes must have the same accessibility, such as public, private etc.

Important Notes:-

  1. If any part of partial class is declared abstract  then whole type is considered abstract.
  2. If any part of partial class is declared sealed then whole type is considered sealed.
  3. If any part declared base type, then the whole type inherits that class.
Example:-
public partial class A
{
    private int x;
    private int y;

    public A(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

public partial class A
{
    public void Print()
    {
        Console.WriteLine("Values: {0},{1}", x, y);
    }

}

class TestCoOrds
{
    static void Main()
    {
        A objA= new A(10, 15);
        objA.Print();

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
// Output: Values: 10,15

Partial Methods

With the introduction of .NET Framework 3.0, the partial family gets a new member partial method, which allows to separate method definition and implementation. 

A partial class may contain a partial method. One part of the class contains the signature of method. An implementation(optional) may be defined in the same part or other part. If the implementation is not supplied, then the method and all calls to the method are removed at compile time.

Partial methods are especially useful as a way to customize generated code. They allow for a method name and signature to be reserved, so that generated code can call the method but the developer can decide whether to implement the method. Much like partial classes, partial methods enable code created by a code generator and code created by a human developer to work together without run-time costs.
partial void Print(); //Declaration in file1.cs
partial void Print() //Implementaion in file2
{
   Console.WriteLine("Method Implementation");
}


Important Notes:-

  1. Partial methods are implicitly private, therefore they can not be virtual.
  2. Partial methods must declare as void return type.
  3. Partial methods can have ref but not out parameters.
  4. Partial methods can have static and unsafe modifiers.
  5. Partial methods can be generic.
  6. Partial method can not be marked as extern.

References:

No comments:

Post a Comment

^ Scroll to Top