0Pricing
C# Academy · Lesson

Static Members and Methods

Understand how to use static members and methods for shared functionality.

Static Members and Methods is a free C# Academy lesson on CoddyKit — lesson 3 of 6. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 6 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Static Members and Methods

Welcome to the next lesson! In this lesson, you’ll learn about static members and methods in C#, their purpose, and how to use them effectively in your programs. Let’s get started!

Static Members and Methods — illustration 1

2

What Are Static Members?

Static members belong to the class itself rather than any specific instance of the class. They are shared among all instances and can be accessed without creating an object of the class.

Key Point: Use static members for values or methods that are the same for all instances.

3

Defining Static Fields

To define a static field, use the static keyword. Example:

Key Point: Static fields are shared across all instances of the class.

using System;

class Counter {
    public static int Count = 0; // Static field

    public Counter() {
        Count++; // Increment count for each object created
    }
}

class Program {
    static void Main() {
        new Counter(); // Create first object
        new Counter(); // Create second object

        Console.WriteLine("Total Count: " + Counter.Count); // Outputs: Total Count: 2
    }
}

4

What Are Static Methods?

Static methods can be called on the class itself without requiring an instance. Use them for utility functions or operations that do not depend on instance data. Example:

Tip: Use static methods for functionality that is not tied to specific objects.

using System;

class MathUtils {
    public static int Add(int a, int b) {
        return a + b;
    }
}

class Program {
    static void Main() {
        int result = MathUtils.Add(5, 3); // Call static method
        Console.WriteLine("Sum: " + result); // Outputs: Sum: 8
    }
}

5

Static Properties

Static properties allow controlled access to static fields. Example:

Key Point: Static properties provide a clean way to access and modify static fields.

using System;

class Configuration {
    private static string appName = "MyApp"; // Static field

    public static string AppName { // Static property
        get { return appName; }
        set { appName = value; }
    }
}

class Program {
    static void Main() {
        Console.WriteLine("App Name: " + Configuration.AppName); // Get static property
        Configuration.AppName = "SuperApp"; // Set static property
        Console.WriteLine("Updated App Name: " + Configuration.AppName);
    }
}

6

Static Constructors

A static constructor is called automatically to initialize static fields or perform other one-time tasks for a class. Example:

Tip: Static constructors cannot have access modifiers or parameters and are called only once.

using System;

class Logger {
    public static string LogFilePath;

    // Static constructor
    static Logger() {
        LogFilePath = "logs.txt"; // Initialize static field
        Console.WriteLine("Static constructor called.");
    }
}

class Program {
    static void Main() {
        Console.WriteLine("Log File Path: " + Logger.LogFilePath);
    }
}

7

Practice Challenge

Create a class TemperatureConverter with static methods CelsiusToFahrenheit() and FahrenheitToCelsius() for temperature conversion. Example:

using System;

class TemperatureConverter {
    public static double CelsiusToFahrenheit(double celsius) {
        return (celsius * 9 / 5) + 32;
    }

    public static double FahrenheitToCelsius(double fahrenheit) {
        return (fahrenheit - 32) * 5 / 9;
    }
}

class Program {
    static void Main() {
        Console.WriteLine("25°C to Fahrenheit: " + TemperatureConverter.CelsiusToFahrenheit(25));
        Console.WriteLine("77°F to Celsius: " + TemperatureConverter.FahrenheitToCelsius(77));
    }
}

8

9

Best Practices for Static Members

Here are some best practices:

  • Use static members for functionality or data that is independent of class instances.
  • Avoid excessive use of static fields to maintain modularity and testability.
  • Use static constructors for initializing static data once per application lifecycle.

Tip: Keep static members simple and focused.

10

Great Job!

Congratulations! You’ve learned how to use static members, methods, and constructors to define class-level functionality in C#. In the next lesson, we’ll explore enums and structs to manage fixed sets of values and group related data. Let’s keep coding!

Static Members and Methods — illustration 10

Frequently asked questions

Is the “Static Members and Methods” lesson free?

Yes — the full text of “Static Members and Methods” is free to read here on the web, and the C# Academy course includes 6 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Static Members and Methods”?

Understand how to use static members and methods for shared functionality. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 6, so you can start here or from the beginning and move at your own pace.

How long does the “Static Members and Methods” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to Interfaces
  2. Abstract Classes
  3. Static Members and Methods
  4. Exploring Enums and Structs
  5. Delegates and Events
  6. Working with Collections: Lists, Dictionaries, and Queues
← Back to C# Academy