C#C
C#11mo ago
Faker

✅ Method declaration, outside Main Vs inside main

Hello guys, consider the following code:

C#
using System;

namespace Learning
{
    class Program
    {
        static void Main(string[] args)
        {
            DisplayRandomNumbers();
            
            void DisplayRandomNumbers()
            {
                Random random = new Random();
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine(random.Next(1,10));
                }
            }
        }

        
    }
}

At first, I wrote the DisplayRandomNumbers() method outside the Main method but I got an error telling me that I can't call the DisplayRandomNumbers from a static context. Can someone explain why I got this error and why if I put the method inside the main method itself, it works fine. What's happening behind the scenes here?
Was this page helpful?