SQL Programmers Blog - Partial Class in C#


  

Need help with Microsoft SQL Server? Ask our Experts, or simply Email your query.

About Sql Server Programmers

Our clients can be found both locally in the Chicagoland area and throughout the country. We have over 18 years of experience and are extremely proud of our track record of successfully assisting hundreds of our clients to improve their productivity while focusing on cost.

Microsoft Certified Partner

  

SQL Server Programmers Blog

Aug 9

Written by: host
8/9/2010 11:47 PM 

Visual Studio .NET 2005, ASP.NET 2.0, shipped with a lesser-known functionality called a “Partial Class”. A Partial class is a class defined in 2 or more files. Each source file contains a section of the class which will combine when the application is compiled.

In previous versions of Visual Studio classes are confined to a single file. It was not possible to split up a single class into multiple files. In order to split up a class we have to use the keyword “Partial”. The purpose of the Partial Class is to allow more than one programmer to write code for the same class, at the same time.

Example

namespace Examples
{
    public partial class Employee
    {
        public void GetName()
        {
            Console.WriteLine("Employee Name is Ram");
        }
    }

    public partial class Employee
    {
        public void getAge()
        {
            Console.WriteLine("Employee Age is 24");
        }
    }
  
    class Program
    {
         
        static void Main(string[] args)
        {
            Employee emp = new Employee();
            emp.GetName();
            emp.getAge();
        }
    }
}

Output

Partial Class

In the above example, the Employee class is split into 2 parts. The first part defines the GetName() method, and the second part defines the GetAge() method. When you compile this program both parts will be combined and compiled.

The important point to remember is both sections use the keyword Partial and the public access modifier.

Points to considered on creating Partial Class

  • All the parts must use the partial keyword.
  • During compile time, all the parts should be available to form the final class.
  • All the parts should have the same access modifiers. – Public, Private, Protected and etc.,
  • Any member declared in the partial class is available to all other parts.
  • If any part is abstract, the entire class is abstract
  • If any part is sealed, then entire class is sealed.
  • If any part has Inheritance, then it applies to the entire class.
  • We can create the Partial Structs, Interfaces and methods in the same way as we create partial classes.
  • Different parts of the partial calls can inherit from different interfaces.

In the above example two parts are declared as public.  In the following example we will create one part as abstract.

namespace Examples
{
    public partial class Employee
    {
        public void GetName()
        {
            Console.WriteLine("Employee Name is Ram");
        }
    }

    abstract partial class Employee
    {
        public void getAge()
        {
            Console.WriteLine("Employee Age is 24");
        }
    }
  
    class Program
    {
         
        static void Main(string[] args)
        {
            Employee emp = new Employee();
        }
    }
}


Output

If you create an object for the Employee class, you will get the following error:

“Cannot create an instance of the abstract class or interface Examples.Employee”. 

This is because the second part is declared as abstract, so the whole class became abstract. Hence you cannot create an object.

We can create nested classes as partial class; even when the main class is not partial.

class MainClass
{
  public partial class Employee
  {
    void getName() { }
  }
  public partial class Employee
  {
    void getAge() { }
  }
}

Partial Methods

Partial methods are methods placed inside partial classes. The implementation is optional. If the implementation is not provided then the method and all the calls to the method are removed at compile time; code inside the partial class can use a partial method.

// Definition in file1.cs
partial void onNameChanged();

// Implementation in file2.cs
partial void onNameChanged()
{
  // method body
}

Important Points in Partial Method

  • Partial method declarations must begin with partial keyword.
  • The return type of the partial class should be void
  • Partial methods cannot have the out parameter, but can have the ref parameter.
  • Partial methods can be private but they cannot be virtual.
  • Partial classes cannot be extern.

Tags:

2 comment(s) so far...

Re: Partial Class in C#

You left out the most important reason why partial classes exist :

When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when creating Windows Forms, Web Service wrapper code, and so on. You can create code that uses these classes without having to edit the file created by Visual Studio.

Reference : msdn.microsoft.com/en-us/library/wa80x488%28VS.80%29.aspx

I don't really see a point in using them for the reason of having multiple programmers working on the same class. That's what Source Control and software like svn does already. The other approach however, has its merits.

By Andrei on   8/19/2010 9:46 AM

Re: Partial Class in C#

A partial class is not defined as 2 or more files but 1 or more classes....you do not need need to provide more than one class as that is optional and neither do they need to be split across multiple files. Thanks Coldeep

By coldeep on   6/27/2011 7:39 AM

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel