/* ------------------------ My Meta Content Here SEO ------------------------ */


Main Menu

Wednesday, July 24, 2013

Partial Classes in C#

Partial classes is a new feature of OOPs in .NET2.0 

Partial classes means split the class into multiple files.
When compiled all the files will be treated as a single class.

it may be helpful in large projects,so many people can work
on same class.


It is especially useful for: Allowing multiple developers to work on a single class at
the same time without the need for later merging files in
source control.

One of the greatest benefits of partial classes is that it 
allows a clean separation of business logic and the user 
interface (in particular the code that is generated by the 
visual designer). 

Using partial classes, the UI code can be
hidden from the developer, who usually has no need to
access it anyway. Partial classes will also make debugging
easier, as the code is partitioned into separate files.


Program that uses partial class: C#

class Program
    static void Main()

Contents of file A1.cs: C#

using System;

partial class A
    public static void A1()

Contents of file A2.cs: C#

using System;

partial class A
    public static void A2()


To split a class definition, use the partial keyword modifier, as shown below:
public partial class Employee
    public void DoWork()

public partial class Employee
    public void GoToLunch()
The partial modifier can only appear immediately before the keywords classstruct, or interface.
Partial Class :
  • We were declaring a class in a single file but Partial class is a feature which allows us to write class across multiple files.
  • The partial indicates that the parts of the class, struct, or interface can be defined in the namespace. All the parts must be used with the partial keyword. All the parts must be available at compile time to form the final type or final class. All the parts must have the same accessibility level, such as public, private, protected, and so on.
  • If any part of the class is declared abstract, then the whole type is considered to be as abstract.
  • If any part is declared sealed, then the whole type is considered to be as sealed.
  • If any part declares a base type, then the whole type inherits that class.
Example: Test1.cs:- namespace PartialClass { public partial class MyTest { private int a; private int b; public void getAnswer(int a, int b) { this.a = a; this.b = b; } } } Test2.cs:- namespace PartialClass { public partial class MyTest { public void PrintCoOrds() { Console.WriteLine("Integer values: {0},{1}", a, b); Console.WriteLine("Addition: {0}", a+b); Console.WriteLine("Mulitiply: {0}", a * b); } } } Program.cs:- namespace PartialClass { class Program { static void Main(string[] args) { MyTest ts = new MyTest(); ts.getAnswer(12, 25); ts.PrintCoOrds(); Console.Read(); } } } OUTPUT: Integer values: 12,25 Addition: 37  Mulitiply: 300
Partial Method :
  • Partial class or struct can contain Partial method.
  • One part of class contains signature or declaration of the method and the implementation or definition of method can be in same class or different class.
  • Partial methods enable the implementer of one part of a class to define a method, similar to an event. The implementer of the other part of the class can decide whether to implement the method or not. If the method is not implemented, then the compiler removes the method signature and all calls to the method .
  • A partial method declaration consists of two parts: 1. definition and 2. Implementation.
  • partial void onNameChanged(); // Implementation in file2.cs partial void onNameChanged() { // method body }
  • Partial methods can have ref but not out parameters.
  • Partial method can have static or unsafe modifiers but can not be extern as presence of body decide whether they are defining or implementing.
  • Partial method can be Generic.
  • Partial methods are implicitly private, and therefore they cannot be virtual.

No comments:

Post a Comment

My Blog List