C# Interview Question #30
What is a partial class in C#?
Classes, Constructors & Properties Junior Beginner
Quick Interview Answer
A partial class allows one class definition to be split across multiple source files. At compile time, the parts are combined into a single type.
Detailed Explanation
A partial class allows one class definition to be split across multiple source files. At compile time, the parts are combined into a single type.
Partial types are useful for generated code, large types, designer-generated files, and scenarios where generated and manually maintained code should be kept separate.
Every part must use the partial keyword and must represent the same type in the same namespace.
Code Example
// Product.cs
public partial class Product
{
public int Id { get; set; }
}
// Product.Validation.cs
public partial class Product
{
public bool IsValid()
{
return Id > 0;
}
}