Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Partial classes in ASP.NET

Author: Richard Wand

This article will give a good explanation of partial classes, their purpose and use of partial classes in ASP.NET.


Partial classes are a technique to split a single class into multiple classes. It means we can split one class file into multiple numbers of physical files with the same name but with the use of “Partial” keyword. This feature is available in ASP.NET to allow multiple developers to work on different parts of the same class from different locations simultaneously. So with the use of partial classes feature, different developers can work on different functionalities in the separate part of the same class. Partial classes also allow clean separation of business logic and user interface.
When partial classes compiled, they do not make any difference to compiler. No modification is done in CLR for the implementation of partial classes. Compiler integrates all partial classes those have the same name and partial keyword into single class and compile it as a single file. Compiler treats partial classes as single entity by grouping them together. We can place all fields and properties in one partial class and all methods in another partial class and we can also place some methods in one partial class and some methods in another partial class. When we create an instance of this class, all the methods declared in partial classes will be available to call. Partial classes feature is a compile time event not a runtime event.
A very good example of partial classes is the use partial classes in ASP.NET applications. When we create an application in Visual Studio 2005 and above with markup and code file separate, we can see our code file declared as partial class with “Partial” keyword. We can access web controls in code file those are defined in markup file. We are able to do this because a partial class is generated automatically with the same name as code file class. This auto generated partial class has properties for web controls that enables us to access web controls in code file using id property.     
Partial Methods
Partial method is also a feature of ASP.NET. Partial method means we can define a method in one partial class and can implement this method in another partial class with the use of “Partial” keyword both with definition and implementation of method. Both parts of the method will be combined and complied by the compiler. Partial methods must be private and we can only declare it in partial classes.
Reasons to use Partial classes
As I have discussed, there are two or three obvious reasons to use partial classes. First, developers can work on different parts of the same class in separate files without sharing the class. This is very beneficial when we have large class files and we have different developers to implement different functionality in the same class  Second reason to use partial classes is to separate business logic and designer generated code. Partial classes are also used when we have to separate code which we want to edit frequently from the code which we don’t want to edit frequently.
Some important points to remember about partial classes
  • You must have to write “Partial” keyword to declare partial classes.
  • Name of the class must be same for all partial classes.
  • The accessibility for all partial classes of a class must be same.
  • Inheritance for one partial class is considered for whole class.
  • If one partial class is declared as “abstract” then whole class is considered as abstract.
  • All partial classes must be available at compile time to implement it as a whole class.
  • Two partial classes cannot refer to same class in different assemblies.
  • Partial classes cannot be declared in different languages for same class.  
Partial class example
public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        class1 obj = new class1();
        int a = obj.func1();
        string var1 = obj.func2();
        Response.Write(var1 + "<br/>" + a);
public partial class class1
    private string var1 = "";
    public int func1()
        a = 5;
        return a;
public partial class class1
    private int a = 0;
    public string func2()
        var1 = "Declared in another partial class";
        return var1;
Partial Class Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim obj As New class1()
        Dim a As Integer = obj.func1()
        Dim var1 As String = obj.func2()
        Response.Write(var1 & "<br/>" & a)
    End Sub
End Class
Partial Public Class class1
    Private var1 As String = ""
    Public Function func1() As Integer
        a = 5
        Return a
    End Function
End Class
Partial Public Class class1
    Private a As Integer = 0
    Public Function func2() As String
        var1 = "Declared in another partial class"
        Return var1
    End Function
End Class
Above example is a simple one to show the use partial classes ASP.NET. We can declare variables, fields and properties in one partial class and can use these in another partial class. When we create an object of a class, methods defined in every partial class can be accessed using this object.  


Add Article Comment:
Name :
Email Address :
Comments :
<< How to update GridView after certain interval using UpdatePanel and Timer Controls in ASP.NET

Disclaimer - Privacy
© 2002-2017