When to use static classes in C# and why they are not always bad?
Hi everyone! I'm a beginner C# developer (studying for about 4 months). I think I've grasped the basics of OOP: classes, objects, inheritance, polymorphism. But there's one topic that confuses me — static classes.
In tutorial examples, I often see them used for helpers or constants. For example, I wrote this class for mathematical operations:
```csharp
public static class MathHelper
{
public static double CircleArea(double radius)
{
return Math.PI * radius * radius;
}
public static double CelsiusToFahrenheit(double celsius)
{
return celsius * 9 / 5 + 32;
}
}
```
But then I read on forums that static classes violate OOP principles (specifically Dependency Injection and testability), and they should be avoided. They say
No answers yet.
Log in to leave an answer
Blogs