- 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?
Give us your anonymous feedback regarding this page, or any other areas of the website.
Related challenges
Add an extension method to a C# class
See if you can add a static extension method to a class and not modify the original type with our C# coding challengeConvert hours, minutes and seconds into a time formatted string
Pass in the hours, minutes and seconds into a method and get it to return a time formatted string which should include leading zeros.
Contains online code editor