C#  

What’s the difference between static, public, and void in C#?

When you see a method defined as:

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.

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:

  • ✅ You don’t need to create an object to use it.
  • ✅ You can call it directly using the class name.
MathUtility.Square(5); // no object created, just using the class

Important Notes:

  • Static methods cannot access instance (non-static) members.
  • They can’t use the this keyword.
  • All static members are shared across the whole program.
  • Static methods are usually used for helper or utility functions.

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.

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:

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

This method uses all three keywords:

  • ✅ public: So the system can find and run it from anywhere.
  • ✅ static: So it runs without needing an object.
  • ✅ void: Because it doesn’t return any value; it just starts your program.

📌 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#

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
    }
}
  • IncrementStatic() operates at the class level, shared across all instances.
  • IncrementInstance() operates on a specific object, each object has its own InstanceCount.

🚫 What Happens If You Mix Them Wrong?

  • ❌ A static method cannot use any instance members like fields or this.
  • ❌ A non-public method like private static void Foo() can only be called from within its own class.
  • ❌ A method declared with a non-void return type must return the correct type.

📝 Summary

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

  • public makes the method accessible from anywhere, inside the same class, other classes, or even other projects.
  • static means the method belongs to the class itself, not to any specific object. You don’t need to create an instance to use it.
  • void shows that the method doesn’t return any value, it just performs an action.

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

  • Global access is needed (public)
  • No object creation is required (static)
  • No result needs to be returned (void)

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.