I
I
Ivan2020-12-29 16:49:10
C++ / C#
Ivan, 2020-12-29 16:49:10

The contents of the method are not displayed. What to do?

I recently noticed that text is not displayed inside the method.
The code is here:

using System;

namespace Application
{
  class Program
  {
    static void Main(string[] args)
    {
      void Method()
      {
        Console.WriteLine("Hello world");
      }
    }
  }
}

I expect that after running this code, Hello world.

If you do this, then everything is ok:
using System;

namespace Application
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Hello world");
    }
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2020-12-29
@Ivan-grozny

In the first case, you only declared a local method. If it is not called, then the code from it will not be executed.
If you want the code from the first option to work - write

using System;
namespace Application
{
  class Program
  {
    static void Main() // Обявили статичный метод Main
    {
      void Method() // Объявили локальный метод внутри Main
      {
        Console.WriteLine("Test");
      }

      Method(); // Вызвали метод
    }
  }
}

F
freeExec, 2020-12-29
@freeExec

- a photo. But outside the method, everything is fine to display:

What do you think Mainit is?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question