Partial Class in C#
Dec 08, 2020 08:19 0 Comments C# (C-Sharp) PARTH

                       Partial Class in C#

When we work on complex projects, it becomes necessary to divide the class into multiple physical files to make sure that multiple developers can work on it at the same time. This concept is known as Partial Class in C#. Partial classes allow a single class file to be split up across multiple physical files, and all these parts are combined when the application is compiled.

For example –

Let’s say we have a class named Product.cs as shown below and it has two methods – AddProduct and DeleteProduct.

public class Product

{

    public bool AddProduct()

    {

    }

    public bool DeleteProduct()

    {

    }

}

Now, say one developer has to work on the logic related to addition of new products to the portal while the other developer has to work on the logic related to deletion of existing product(s) from the portal.

So, in this case we divide the Product class into two separate partial classes as shown below –

Product1.cs:

public partial class Product

{

    public bool AddProduct ()

    {

    }

}

Product2.cs:

public partial class Product

{

    public bool DeleteProduct ()

    {

    }

}

Advantages of using Partial Class :

  1. The biggest advantage of using partial classes is that it enables multiple programmers to work on the same class parallelly. 
  2. It’s also easier to categorize code with the help of partial classes.

 

Some important points related to Partial Class :

  1. The partial keyword indicates that other parts of the class can be defined in the same namespace. 
  2. All the parts should use the partial keyword, also, all the parts should be available at compile time to form the final type.
  3. All the parts should have the same accessibility, such as public, private, and so on.
  4. If you inherit a class or interface on a partial class, then it should be inherited on all parts of a partial class.
  5. If a part of the partial class is sealed , then the entire class will be sealed.
  6. If a part of the partial class is abstract, then the entire class will be an abstract class.
Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect