Introduction:

In this article we will learn what is a partial class in C# language or how to use partial class in c# or How to a partial class separates the definition of a class over two or more source files with example.

Description:

A partial class separates the definition of a class over two or more source files. You can create a class definition in multiple files but it will be compiled as one class.

Suppose you have a "customer" class. That definition is divided into the two source files "customer1.cs" and "customer2.cs". Then these two files have a class that is a partial class. You compile the source code, then create a single class. Let's see that in Figure 1.1.

Partial Class in C# with Example


  • Syntax:

 

partial class MyPartClass // Same class name as
following
{
member1 declaration
member2 declaration
...
}
 
partial class MyPartClass // Same class name as preceding
{
member3 declaration
member4 declaration
...
}

Example:

Now we have created two source files "customer1.cs" and customer2.cs

customer1.cs

public partial class Customer
    {
       public int ID { get; set; }
        public string Name { get; set; }
    }

customer2.cs

public partial class Customer
    {
        public string EmailID { get; set; }
        public string Contact { get; set; }
    }

 

Now when we create an object of Customer class then customer1.cs and customer2.cs compile the source code then create a single class and both members can access in a single object, see below.

using Example;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Partial_Class
{   
    class Program
    {
        public static void Main(string[] args)
        {
            Customer cust = new Customer();
            cust.ID = 1;
            cust.Name = "vinay Kumar";
            cust.EmailID = "vinay@sqltutorialspoint.com";
            cust.Contact = "1222*******";
        }
    } 
}

 

  • Advantages of Partial Class 
  • 1. By Using Partial Classes, multiple developer can work on the same class easily.
  • 2. Partial classes are mainly used by code generator to keep different concerns separate
  • 3. you can also define Partial methods as well where a developer can simply define the method and     the other developer can implement that.

    • Notes:
      • 1. Use partial keyword in each part of partial class.
      • 2. Name of each part of partial class should be the same but source file name for each part of     partial class can be different.
      • 3. All parts of a partial class should be in the same namespace.
      • 4. Each part of a partial class should be in the same assembly or DLL, in other words you can’t     create a partial class in source files of a different class library project.
      • 5. Each part of a partial class has the same accessibility. (like private, public or protected)
      • 6. If you inherit a class or interface on a partial class then it is inherited on all parts of a partial        class.
      • 7. If a part of a partial class is sealed then the entire class will be sealed.
      • 8. If a part of partial class is abstract then the entire class will be considered an abstract class.

      • I hope this article will help to you.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like