- Home
- .NET coding challenges
- Using a partial class in C#
Using a partial class in C#
By using partial classes in C#, we can split a class over two or more files.
This can be helpful to split out functionality across multiple files. In-addition, code can be added to an automatically generate class code without having to recreate it.
There is more information about partial classes on the Microsoft website.
Take this code:
public class Currency
{
public string? CurrencySymbol { get; }
}
public class Currency
{
public decimal? UsdCurrencyConversion { get; }
}
As there are two Currency
classes in the same namespace, this would not compile.
How would we make it compile without changing either of the class names?
Related challenges
Why does inheritance in a C# class not always compile?
A coding challenge to find out why class inheritance in C# doesn't always compile a .NET application and how to resolve it.Write a C# function to convert MPH to KPH
Take our coding challenge to write a C# function with the necessary parameters that converts miles-per-hour (MPH) to kilometres-per-hour (KPH) and vice-versa.
Contains online code editor