Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between public, static and void keywords in C#?
The public, static, and void keywords in C# have specific meanings and are commonly seen together in the Main method of any C# program. Understanding these keywords is essential for C# programming as they control access, memory allocation, and return behavior of methods.
The Main method serves as the entry point for all C# programs, defining what a class does when executed.
Syntax
Following is the typical syntax of the Main method showing all three keywords −
public static void Main(string[] args) {
// program execution starts here
}
Understanding Each Keyword
public Keyword
The public keyword is an access modifier that makes the method accessible from anywhere in the program or even from other assemblies.
using System;
class Demo {
public static void Main(string[] args) {
Console.WriteLine("Main method is public - accessible everywhere");
PublicMethod();
}
public static void PublicMethod() {
Console.WriteLine("This public method can be called from anywhere");
}
}
The output of the above code is −
Main method is public - accessible everywhere This public method can be called from anywhere
static Keyword
The static keyword means the method belongs to the class itself rather than to any specific instance. You can call static methods without creating an object of the class.
using System;
class Calculator {
public static int Add(int a, int b) {
return a + b;
}
public int Multiply(int a, int b) {
return a * b;
}
public static void Main(string[] args) {
// Static method - no object needed
int sum = Add(5, 3);
Console.WriteLine("Sum: " + sum);
// Non-static method - object required
Calculator calc = new Calculator();
int product = calc.Multiply(5, 3);
Console.WriteLine("Product: " + product);
}
}
The output of the above code is −
Sum: 8 Product: 15
void Keyword
The void keyword indicates that the method does not return any value. Methods with void perform actions but don't send back data to the caller.
using System;
class ReturnExample {
public static void DisplayMessage() {
Console.WriteLine("This void method returns nothing");
}
public static string GetMessage() {
return "This method returns a string";
}
public static int GetNumber() {
return 42;
}
public static void Main(string[] args) {
DisplayMessage(); // void method
string message = GetMessage(); // returns string
Console.WriteLine(message);
int number = GetNumber(); // returns int
Console.WriteLine("Number: " + number);
}
}
The output of the above code is −
This void method returns nothing This method returns a string Number: 42
Comparison of Access Modifiers
| Access Modifier | Accessibility | Description |
|---|---|---|
| public | Everywhere | No access restrictions |
| private | Same class only | Most restrictive access |
| protected | Same class and derived classes | Accessible in inheritance hierarchy |
| internal | Same assembly | Accessible within the same project |
Why Main Method Uses All Three Keywords
-
public− The CLR (Common Language Runtime) needs to access the Main method from outside the class to start program execution. -
static− The CLR calls Main without creating an instance of the class, so it must be static. -
void− Main doesn't need to return a value to the CLR in most cases (thoughintreturn is also allowed for exit codes).
Conclusion
The public, static, and void keywords serve distinct purposes: public controls accessibility, static eliminates the need for object instantiation, and void indicates no return value. Together in the Main method, they enable the CLR to execute your program's entry point.
