How does the Main method actually work?

Hello everyone.

I have a simple, yet for my understanding of the language C# crucial question. Better to ask in code form:

static void Main(string[] args)
    {
      VisitPlanets();
    }
    static void VisitPlanets()
    {
      Console.WriteLine("Hi there.");
    }

Here the static void VisitPlanets() needs to be included into the Main() in order to run. Do I need to do this with EVERY method I define myself?

Hello, @ruby6085260168.

The Main() method is where program execution begins. The VisitPlanets() method need not be included inside of the Main() method. You could do this for example:

using System;

namespace Examples
{
  class Program
  {

    static void VisitPlanets()
    {
      Console.WriteLine("Hi there.");
    }

    static void Main(string[] args)
    {
      VisitPlanets();
    }
  }
}

As you progress through C#, you’ll learn how to access methods, classes, etc. that are not inside of the Main() method. They may even be in totally separate files from the main method. :slightly_smiling_face:

Hi @midlindner

Thanks for the reply. Really appreciate it. Now, to say it clear. There is no need to do it. But right now it’s the only way to get a method to run we know.

Thanks a lot. Thums up for that.

1 Like