C# C# Basics (Retired) Prepare and Plan Program Structure

divanj
divanj
716 Points

What is static in static void main(); ? main() is the method, void the blank return type. How about static?

Title says it all.

Having completed C# basics, I've understood ever code I've typed - except that word static. So what is static in the main method header?

2 Answers

Seth Kroger
MOD
Seth Kroger
Treehouse Moderator 56,275 Points

A static method belongs to the class itself and not an instance. You'll sometimes hear them called class methods. The static keyword allows you to call the method from the class itself without having to create an object instance.

This is particularly useful for main() because this is the very beginning of the program and you've created no objects yet to call instance methods on.

Ari Misha
Ari Misha
19,220 Points

Hiya there! A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself. Hence you cant create objects on it. I hope it helped. (: