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!

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay