Home > Enterprise >  Why does Visual Studio not complain when I write Console.WriteLine?
Why does Visual Studio not complain when I write Console.WriteLine?

Time:10-06

I was recently watching tutorials on C# where they mentioned that specifying either using System and then using Console.WriteLine or writing System.Console.WriteLine if you don't specify using <namespace> was necessary for proper namespace resolution to happen when writing programs otherwise the compilation would fail. But when I'm using Visual Studio 2022, Console.WriteLine seems to work without specifying any namespace at all.

Is this some optimisation that Visual Studio 2022 does? Why is this working? On top of this, if I use System.Console.WriteLine, VS 2022 also greys out System and says "name can be simplified".

CodePudding user response:

If you create c# project of asp.netframrwork. It will automatically generate the "using System" namespace.

enter image description here

If your console project is .NET 6 and above. Please refer to Exploring Ideas to Build Code While Learning Using Top-Level Statements.

Hope it helps you.

CodePudding user response:

Try this

using System;
using blablabla

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
  • Related