Static Classes
Part of the Object Oriented Programming section of Coddy's C# journey — lesson 13 of 70.
When a class contains only static members and you never need to create instances of it, you can declare the entire class as static. This tells the compiler that the class cannot be instantiated and can only contain static members.
public static class MathHelper
{
public static double Pi = 3.14159;
public static int Square(int number)
{
return number * number;
}
public static int Add(int a, int b)
{
return a + b;
}
}You use static classes the same way you use static members, by calling them directly on the class name:
int result = MathHelper.Square(5);
double pi = MathHelper.Pi;Static classes have important restrictions. You cannot create an instance with new, they cannot have instance constructors, and they cannot be inherited. They can only contain static fields, properties, and methods.
The Console and Math classes you've been using are perfect examples of static classes. They provide utility functionality without needing object state. Static classes are ideal for grouping related helper methods or constants that don't require any instance-specific data.
Challenge
EasyLet's build a temperature conversion utility using a static class to group related helper methods that don't require any object state.
You'll create two files to organize your code:
TemperatureConverter.cs: Define a static class calledTemperatureConverterin theUtilitiesnamespace. This class should contain:- A static field
AbsoluteZeroCelsiusset to-273.15 - A static method
CelsiusToFahrenheit(double celsius)that converts Celsius to Fahrenheit using the formula:(celsius * 9 / 5) + 32 - A static method
FahrenheitToCelsius(double fahrenheit)that converts Fahrenheit to Celsius using the formula:(fahrenheit - 32) * 5 / 9 - A static method
IsAboveAbsoluteZero(double celsius)that returnstrueif the temperature is above absolute zero
- A static field
Program.cs: In your main file, use the static class to perform conversions. Read a temperature value in Celsius from input, then display the conversions and validation.
You will receive one input: a temperature value in Celsius.
Print the results in this format:
{celsius}C = {fahrenheit}F
{fahrenheit}F = {convertedBack}C
Above Absolute Zero: {True/False}
Absolute Zero: {AbsoluteZeroCelsius}CFor example, if the input is 25, the output should be:
25C = 77F
77F = 25C
Above Absolute Zero: True
Absolute Zero: -273.15CRemember that static classes cannot be instantiated - you'll call all methods directly on the class name like TemperatureConverter.CelsiusToFahrenheit(25).
Cheat sheet
A static class can only contain static members and cannot be instantiated. It's used to group related utility methods and constants that don't require object state.
public static class MathHelper
{
public static double Pi = 3.14159;
public static int Square(int number)
{
return number * number;
}
public static int Add(int a, int b)
{
return a + b;
}
}Static class members are accessed directly on the class name:
int result = MathHelper.Square(5);
double pi = MathHelper.Pi;Restrictions of static classes:
- Cannot be instantiated with
new - Cannot have instance constructors
- Cannot be inherited
- Can only contain static fields, properties, and methods
Common examples include Console and Math classes, which provide utility functionality without needing object state.
Try it yourself
using System;
using Utilities;
class Program
{
public static void Main(string[] args)
{
// Read the temperature in Celsius
double celsius = Convert.ToDouble(Console.ReadLine());
// TODO: Use TemperatureConverter static methods to:
// 1. Convert celsius to fahrenheit
// 2. Convert fahrenheit back to celsius
// 3. Check if temperature is above absolute zero
// 4. Get the absolute zero constant
// TODO: Print the results in the required format:
// {celsius}C = {fahrenheit}F
// {fahrenheit}F = {convertedBack}C
// Above Absolute Zero: {True/False}
// Absolute Zero: {AbsoluteZeroCelsius}C
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesNamespaces & DirectivesIntro to Classes & ObjectsThe 'this' KeywordMethods and ParametersFields vs PropertiesConstructorsObject InitializersRecap - Simple Calculator4Inheritance
Basic Inheritance (:) SyntaxThe 'base' KeywordVirtual & Override KeywordsSealed ClassesThe 'object' Base ClassRecap - Employee Hierarchy7Advanced Features
Operator OverloadingIndexers (this[])ToString() OverrideExtension MethodsRecap - Custom List2Properties & Static Members
Auto-Implemented PropertiesRead/Write-Only PropertiesStatic Fields & MethodsStatic ClassesExpression-Bodied Members5Polymorphism & Interfaces
Compile vs Runtime PolyInterface vs Abstract ClassMultiple InterfacesExplicit InterfacesUpcasting & DowncastingRecap - Shape Calculator