Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Sep 30, 2012, 2:26 PM EDT
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 28, 2023, 3:42 AM EST
Static classes are used in programming to create utility classes that contain static members (fields, methods, properties, or events) that can be accessed without the need to create an instance of the class. Here are some reasons why we use static classes:
Example:
Let's say you are building a game and you want to keep track of the score. You could create a static class called ScoreManager that contains a static field for the score and static methods to update and retrieve the score. Here's an example implementation:
public static class ScoreManager { private static int score = 0; public static void AddPoints(int points) { score += points; } public static void ResetScore() { score = 0; } public static int GetScore() { return score; } }You can then access the ScoreManager from anywhere in your code, without the need to create an instance of the class:
ScoreManager.AddPoints(10); int currentScore = ScoreManager.GetScore(); ScoreManager.ResetScore();