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!

💡 One last tip before you go

Tired of spending so much on your side projects? 😒

Just one of many great perks of being part of the network ❤️

Top comments (0)

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay