When you see a method defined as:

C#
public static void MyMethod() { }

It’s actually made up of three separate keywords, and each of them plays an important role. Let’s break them down one by one:

1️⃣ public - Access Modifier

What it means: Public allows the method to be accessed from anywhere in your code, other classes, other files, and even other projects if they reference this one.

Without public: The method becomes limited in visibility, usually only accessible within the same class or project.

C#
public void Greet() {
    Console.WriteLine("Hello!");
}

This Greet() method can be called from other places freely.

2️⃣ static - Class-Level Member

What it means: Static means the method belongs to the class itself, not to a specific object of that class.

That means:

C#
MathUtility.Square(5); // no object created, just using the class

Important Notes:

3️⃣ void - No Return Value

What it means: Void tells us that the method doesn’t return anything. It just performs an action.

If you want the method to give something back (like a number or text), you replace void with a return type like int, string, etc.

C#
public void SayHello() {
    Console.WriteLine("Hello!");
}

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

🔍 Why They Appear Together: The Main() Method

This common method used as the starting point of every console app:

C#
public static void Main(string[] args)
{
    // Application starts here
}

This method uses all three keywords:

📌 Public vs Static vs Void

Keyword Meaning Usage Context Example
public Access modifier that allows access from any other class or assembly Used with classes, methods, fields public int age;
static Belongs to the class itself, not an instance Used with methods, fields, classes public static int count;
void Return type indicating no return value from a method Used with methods public void PrintMessage()

⚙️ Example: Static vs Instance Methods in C#

C#
public class Example
{
    public static int StaticCount = 0;   // Shared across all
    public int InstanceCount = 0;        // Unique to each object

    public static void IncrementStatic()
    {
        StaticCount++;
    }

    public void IncrementInstance()
    {
        InstanceCount++;
    }
}

class Program
{
    static void Main()
    {
        // Static method call (no object needed)
        Example.IncrementStatic();
        Console.WriteLine(Example.StaticCount); // Output: 1

        // Instance method call (object needed)
        Example ex = new Example();
        ex.IncrementInstance();
        Console.WriteLine(ex.InstanceCount); // Output: 1
    }
}

🚫 What Happens If You Mix Them Wrong?

📝 Summary

In C#, the method declaration public static void uses three important keywords, each with a specific purpose:

These keywords are often used together for methods like Main() or utility methods where:

By understanding these three keywords, you can write cleaner, more structured code and know exactly who can access your method, how it's used, and what it returns.