Static Classes in Practice
Static classes play a key role in C# by providing a way to group related utility methods and constants without the need to create an object instance. When you want to organize functions or values that are conceptually tied to a type, but do not depend on object state, a static class is the right tool. Static classes are commonly used for things like logging, mathematical calculations, or holding configuration values that should be accessible throughout your application.
A static class is a class that cannot be instantiated and can only contain static members. All methods, properties, and fields in a static class must be marked as static. Static classes are sealed and cannot be used as a base class.
Program.cs
1234567891011121314151617181920212223242526272829303132using System; namespace ConsoleApp { public static class Logger { public static void Info(string message) { Console.WriteLine("[INFO] " + message); } public static void Warning(string message) { Console.WriteLine("[WARNING] " + message); } public static void Error(string message) { Console.WriteLine("[ERROR] " + message); } } public class Program { public static void Main(string[] args) { Logger.Info("Application started."); Logger.Warning("Low disk space."); Logger.Error("Unhandled exception occurred."); } } }
In this example, the Logger class is declared as static. This means you cannot create an instance of Logger, and all its members must also be static. You use the static methods directly by calling Logger.Info, Logger.Warning, or Logger.Error from anywhere in your code. This pattern is useful because logging is a cross-cutting concern: you want to log messages from many parts of your application, but you do not need to track any state or create multiple logger objects. By making Logger static, you signal that it is a utility class, and you avoid unnecessary object creation.
Constants.cs
123456789namespace ConsoleApp { public static class Constants { public const string ApplicationName = "MyApp"; public const int MaxUsers = 100; public const double TaxRate = 0.07; } }
When deciding between static and instance classes, keep these tips in mind:
- Use a static class when you want to group methods or values that do not require object state;
- Prefer static classes for utility functions, helpers, or constants that are shared across your application;
- Use instance classes when you need to store data that is unique to each object, or when you want to use object-oriented features like inheritance or interfaces.
1. What is a static class?
2. Why can't you create an instance of a static class?
3. When is it appropriate to use a static class?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you give more examples of when to use static classes in C#?
What are the limitations of static classes compared to instance classes?
How do I convert an existing class to a static class in C#?
Awesome!
Completion rate improved to 4.17
Static Classes in Practice
Swipe to show menu
Static classes play a key role in C# by providing a way to group related utility methods and constants without the need to create an object instance. When you want to organize functions or values that are conceptually tied to a type, but do not depend on object state, a static class is the right tool. Static classes are commonly used for things like logging, mathematical calculations, or holding configuration values that should be accessible throughout your application.
A static class is a class that cannot be instantiated and can only contain static members. All methods, properties, and fields in a static class must be marked as static. Static classes are sealed and cannot be used as a base class.
Program.cs
1234567891011121314151617181920212223242526272829303132using System; namespace ConsoleApp { public static class Logger { public static void Info(string message) { Console.WriteLine("[INFO] " + message); } public static void Warning(string message) { Console.WriteLine("[WARNING] " + message); } public static void Error(string message) { Console.WriteLine("[ERROR] " + message); } } public class Program { public static void Main(string[] args) { Logger.Info("Application started."); Logger.Warning("Low disk space."); Logger.Error("Unhandled exception occurred."); } } }
In this example, the Logger class is declared as static. This means you cannot create an instance of Logger, and all its members must also be static. You use the static methods directly by calling Logger.Info, Logger.Warning, or Logger.Error from anywhere in your code. This pattern is useful because logging is a cross-cutting concern: you want to log messages from many parts of your application, but you do not need to track any state or create multiple logger objects. By making Logger static, you signal that it is a utility class, and you avoid unnecessary object creation.
Constants.cs
123456789namespace ConsoleApp { public static class Constants { public const string ApplicationName = "MyApp"; public const int MaxUsers = 100; public const double TaxRate = 0.07; } }
When deciding between static and instance classes, keep these tips in mind:
- Use a static class when you want to group methods or values that do not require object state;
- Prefer static classes for utility functions, helpers, or constants that are shared across your application;
- Use instance classes when you need to store data that is unique to each object, or when you want to use object-oriented features like inheritance or interfaces.
1. What is a static class?
2. Why can't you create an instance of a static class?
3. When is it appropriate to use a static class?
Thanks for your feedback!