Partial Class in C# with Examples

Shahzad Aslam
5 min readJun 9, 2024

We can split a class, a struct, a method, or an interface into multiple parts with the same class name into .cs file and with partial keyword. Partial class was first introduced in C# 2.0. We use a specifc keyword to making partial class that is called partial.

Partial Class in C# with Examples

Partial classes are very useful for organizing large classes, becasue whenever you will have large classes, the code inside these large class will be mized up and it will have lack of clean orginization. so keeping clean organization of code we can split a larage class into small parital classes.

Partial classes also allowing multiple developers to work on different parts of a class simultaneously without conflicts.

We have a file names as FruitFileOne.cs with a partial class named as Fruit.It has one integer variable FruitPrice, one string variable FruitName and one bool variable FruitIsInMarket. we have a constructor named as Fruit which is assigning the values of price & name & Isavailable.

// FruitFileOne.cs

public partial class Fruit
{
// Fields (attributes or variables)

private int FruitPrice;
private string FruitName;
private bool FruitIsInSeason;

// Constructor
public Fruit(int price, string name, bool Isavailable)
{
FruitPrice = price;
FruitName = name;
FruitIsInSeason = Isavailable;
}
}

--

--

Shahzad Aslam

Welcome to c-sharptutorial.com. I am Shahzad Aslam. I am the founder of c-sharptutorial.com. I'm currently living near Islamabad, Pakistan.