In C#, a static class is a class declared using the static keyword. It can contain only static members such as fields, methods, properties and events. A static class cannot be instantiated or inherited and all its members are accessed through the class name.
Static classes are commonly used to group utility or helper methods that do not depend on object state.
Syntax:
static class Class_Name
{
// static data members
// static method
}
Key Points
- A static class is declared using the static keyword.
- All members inside a static class must be static.
- It cannot be instantiated using the new keyword.
- It is implicitly sealed, so it cannot be inherited.
- A static class can only have the public or internal access modifiers (it cannot be private, protected, etc.).
Declaring a Static Class
A static class is defined like a normal class but prefixed with the static keyword.
C#
using System;
static class Utility
{
public static void PrintMessage(string msg)
{
Console.WriteLine(msg);
}
}
class Program
{
static void Main()
{
Utility.PrintMessage("Hello World");
}
}
Explanation:
- Utility is static, so no objects can be created.
- Its method is called directly with the class name.
Static Fields and Properties in a Static Class
Static classes can hold static fields and properties that maintain shared data for the application.
C#
using System;
static class Config
{
public static string AppName { get; set; } = "MyApp";
public static int Version { get; set; } = 1;
}
class Program
{
static void Main()
{
Console.WriteLine(Config.AppName);
Console.WriteLine("Version: " + Config.Version);
}
}
- AppName and Version are shared across the program.
- They are accessed without creating objects.
Static Constructor in a Static Class
A static constructor initializes static data in a static class. It is called only once, automatically, before the class is used.
C#
using System;
static class Logger
{
public static string FilePath;
static Logger() // static constructor
{
FilePath = "C:\\Logs\\app.log";
Console.WriteLine("Logger initialized");
}
}
class Program
{
static void Main()
{
Console.WriteLine("Log Path: " + Logger.FilePath);
}
}
- The static constructor initializes FilePath.
- It runs once before Logger is accessed.
Restrictions of Static Classes
- Cannot be instantiated with new.
- Cannot contain instance members.
- Cannot inherit or be inherited.
- Cannot implement interfaces.
Use Cases of Static Classes
- Utility Methods: Provide helper functionality (e.g., Math.Sqrt).
- Configuration: Store global application settings.
- Logging: Maintain centralized logging functionality.
Difference between static and non-static class
| Static Class | Non-Static Class |
|---|
| Static class is defined using static keyword. | Non-Static class is not defined by using static keyword. |
| In static class, you are not allowed to create objects. | In non-static class, you are allowed to create objects using new keyword. |
| The data members of static class can be directly accessed by its class name. | The data members of non-static class is not directly accessed by its class name. |
| Static class always contains static members. | Non-static class may contain both static and non-static methods. |
| Static class does not contain an instance constructor. | Non-static class contains an instance constructor. |
| Static class cannot inherit from another class. | Non-static class can be inherited from another class. |
When is a static constructor called in C#?
-
-
When the first object of the class is created
-
Automatically before any static member is accessed
-
Only when explicitly invoked
Explanation:
A static constructor is automatically invoked once, before any static data or method of the class is accessed.
Which of these is NOT true about a static class?
-
-
Can contain only static members
-
Can be inherited by another class
-
Explanation:
Static classes cannot be inherited; they are sealed by default.
Which of the following is true about a static constructor?
-
-
Can be called directly by user
-
Runs once automatically before first use
-
Must have public access modifier
Explanation:
A static constructor runs only once automatically before the class is used.
Which of the following statements about a static class in C# is TRUE?
-
A static class can be instantiated using the new keyword.
-
A static class can contain both static and non-static members.
-
A static class is implicitly sealed and cannot be inherited.
-
A static class can implement interfaces.
Quiz Completed Successfully
Your Score : 2/4
Accuracy : 0%
Login to View Explanation
1/4
1/4
< Previous
Next >
Explore
Introduction
Fundamentals
Control Statements
OOP Concepts
Methods
Arrays
ArrayList
String
Tuple
Indexers