Learn Partial Class in C# With Simple Example

A Partial Class is a special feature of C#. Partial Class provides a special ability to implement the functionality of a single class into multiple files. All these files are combined into a single class file when the application is compiled. A partial class is created by using the partialĀ keyword. This keyword is also useful to split the functionality of methods, interfaces, or structure into multiple files.

Partial Class

Now let us try to understand partial classes with the help of a scenario.

Let’s assume we have two Developers (Dev1 & Dev2). Dev1 is assigned to work on Product class with Product Details functionality, whereas Dev2 is also assigned to work on Product class with Product Stock Quantity functionality. Now, how can both these developers work simultaneously on the same class? The solution for this problem is Partial Classes. Dev1 and Dev2 can both work on the same Product class but on different files. Let us understand this with an example.

I will create a console application and will add two class files Dev1.cs and Dev2.cs. I will create a partial class Product in both these class files (Dev1.cs and Dev2.cs).


Lets add Product Details functionality in Dev1.cs and Product Stock functionality in Dev2.cs.

    partial class Product
        public string Name { get; set; }
        public decimal Price { get; set; }

        public string GetProductDetails()
            return "Product Name is: " + Name + " and Product Price is: " + Price;

    partial class Product
        public int StockQty { get; set; }

        public string GetQuantity()
            return "Product Quantity is: " + StockQty;

Let’s now call both these functions in Program.cs

    class Program
        static void Main(string[] args)
            Product product = new Product();

            product.Name = "Soap";
            product.Price = 10;
            product.StockQty = 100;



Now, we will run our application to see the output.


We have combined the work of both Developers (Dev1 and Dev2), which we assume they worked simultaneously on same class “Product” but on different files “Dev1.cs” and “Dev2.cs” and called the methods which they have created in our Program.cs. Thus taking the advantage of Partial Class feature of C#.


Share on facebook
Share on twitter
Share on linkedin

Related articles