C#
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented C#
-
Section 4: Working with Data
-
Section 5: Error Handling
-
Section 6: Delegates and Events
-
Section 7: Async Programming
-
Section 8: More Language Features
-
Section 9: File I/O and Serialization
-
Section 10: Networking in .NET
-
Section 11: The .NET Ecosystem
-
Section 12: Memory and Performance
-
Section 13: Concurrency Beyond Async
-
Section 14: Reflection and Attributes
-
Section 15: Testing and Best Practices
-
Section 16: Design Patterns in C#
-
Section 17: Standard Library Deep Dive
-
Section 18: Data Structures and Algorithms in C#
-
Section 19: GUI and Desktop Development Overview
-
Section 20: Practical Projects
-
Section 21: More Practice Exercises
-
Section 22: More Standard Library and Text Processing
-
Section 23: More Design Patterns
-
Section 24: More Projects
-
Section 25: Interview Practice
-
Section 26: C# Keywords Reference (Modifiers)
-
Section 27: C# Keywords Reference (Statements)
-
Section 28: C# Keywords Reference (Operators)
-
Section 29: BCL: System.Collections.Generic
-
Section 30: BCL: System.Linq
-
Section 31: BCL: System.Threading
-
Section 32: BCL: System.IO
-
Section 33: BCL: System.Text and System.Text.Json
-
Section 34: BCL: System.Net.Http
-
Section 35: C# Language Specification Topics
-
Section 36: More Practice Exercises
-
Section 37: More Async Patterns
-
Section 38: More BCL: System.Reflection and System.Diagnostics
-
Section 39: Nullable Reference Types In Depth
-
Section 40: C# Records and Pattern Matching In Depth
-
Section 41: Dependency Injection Deep Dive
-
Section 42: More Interview and Whiteboard Practice
164: The static Modifier In Depth
You've probably seen the static keyword since the very first line of code you wrote in this course—static void Main—but there is a persistent myth that hangs around as learners move into intermediate C#. A lot of people assume that static means "constant" or "unchangeable." They see a static variable and think it's just a fancy way of doing a const.
That is completely wrong. Let me show you why.
public class GameSettings
{
public static int VolumeLevel = 50;
}
// Later in the code...
GameSettings.VolumeLevel = 80; // This works perfectly.
GameSettings.VolumeLevel++; // This also works.
If static meant "constant," the compiler would have stopped me the moment I tried to change the volume. But it didn't. The VolumeLevel changed across the entire application. The misconception is confusing mutability (whether a value can change) with scope (who owns the value).
"Static means Constant" vs. "Static means Type-Owned"
When you mark a member as static, you aren't telling C# to lock the value; you're telling it that this member belongs to the Type itself, rather than to a specific instance of that type.
Think of a class as a blueprint for a house. A non-static field, like int numberOfWindows, is a property of the specific house you built. Every house can have a different number of windows. But a static field is like the BlueprintVersion written in the margin of the blueprint. It doesn't matter how many houses you build from that blueprint; they all refer back to that one single version number on the original paper.
I've seen juniors struggle with this when they try to track a "Total Players Joined" counter in a multiplayer game. If they make the counter a regular instance field, every new player object starts their own counter at zero. By making it static, every player instance shares the exact same counter in memory.
"Static is for Data" vs. "Static Classes for Pure Logic"
You can have static members inside a normal class, but you can also make the entire class static. This is where things get interesting. A static class cannot be instantiated—you can't use the new keyword on it. It essentially becomes a container for a group of related tools.
I generally reserve static classes for "pure functions"—logic that takes an input, returns an output, and doesn't need to remember anything about the past. The System.Math class is the gold standard here. You don't need to "create a Math object" to calculate a square root; you just ask the Math type to do it for you.
public static class PhysicsHelper { public static double CalculateGravity(double mass) { return mass * 9.81; } }"Static is Convenient" vs. "The Global State Trap"
Here is the professional advice I give to my mentees: just because you can make something static doesn't mean you should. Static members create "global state." This sounds great at first because you can access
GameSettings.VolumeLevelfrom anywhere in your project without passing references around.But global state is a nightmare for testing. If you have a static variable that tracks the state of a user's login, and you write a unit test that changes that state, every subsequent test in your suite is now running with that modified state. You'll find yourself chasing "ghost bugs" where a test fails not because the code is broken, but because a previous test left the static variable in a weird state. Use static for constants or stateless utilities, but be very cautious using it for data that changes frequently.
📋 Practical Task
Exercise: Implementing a Global Game-Time Synchronizer
You are building a game where multiple entities (Players, NPCs, Projectiles) need to know the total time elapsed since the game started. Instead of every entity tracking its own timer, you need a centralized system.
Your Task:
- Create a
static classnamedGameClock. - Inside
GameClock, create astaticfield (or property) calledElapsedSecondsthat stores adouble. - Implement a
staticmethod calledTick(double deltaTime)that adds the provideddeltaTimeto theElapsedSeconds. - Create a regular class called
GameEntity. It should have a methodPrintTime()that prints the current value ofGameClock.ElapsedSecondsto the console. - In your main program, instantiate two different
GameEntityobjects. CallGameClock.Tick(1.5), then callPrintTime()on both entities to verify they both see the same updated time.
There are no comments for now.