DEV Community

DotNet Full Stack Dev
DotNet Full Stack Dev

Posted on

Static Keyword in C# Explained in 1 Minute

✔ What Does static Mean?

The static keyword makes a member (method, property, field, or constructor) or a class belong to the type itself,
not to any specific instance.

Where Is static Used?

Static Fields
    ✅ Shared across all instances of a class.
    Example: A counter tracking all created objects.

Static Methods
    ✅ Can be called without creating an object.
    Example: Utility functions like Math.Sqrt().

Static Properties
    ✅ Provide global access to values related to the class.
    Example: A configuration value.

Static Classes
    ✅ Used for grouping related static methods and properties. Cannot be instantiated.
    Example: Path class in System.IO.

Static Constructors
    ✅ Initialize static fields and execute only once before the class is accessed.
Enter fullscreen mode Exit fullscreen mode

Why Use static?

✅ Saves memory by sharing members across all objects.
✅ Provides global utility or shared behavior.
✅ Eliminates the need for object creation when unnecessary.
Enter fullscreen mode Exit fullscreen mode

Outcome:
static is essential for shared data and utility logic, simplifying and optimizing your code for scenarios where object instances aren’t needed!

Top comments (0)