8

I've got a console application that I just made in Visual Studio for Mac and in the Program.cs the code that was created automatically apparently had errors. Here is the code:

using System;

namespace Console
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

And because it put it there not me, you would expect it to work, but it doesn't, it comes up with this error:

Console/Program.cs(13,13): Error CS0234: The type or namespace name 'WriteLine' does not exist in the namespace 'Console' (are you missing an assembly reference?) (CS0234) (Console)

How do fix this?

Thanks!

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188

1 Answers1

26

As your namespace is called Console, when you call WriteLine, it is trying to find a class within your namespace and overriding the default System.Console.

Either rename you namespace OR explicitly specify the namespace for Console which is in the System namespace:

 System.Console.WriteLine("Hello World!");
mjwills
  • 23,389
  • 6
  • 40
  • 63
Glynn Hurrell
  • 602
  • 6
  • 10
  • 1
    Another alternative is to use `using static System.Console;`. Then you can simply write `WriteLine("Hello World!");` – Olivier Jacot-Descombes Jul 18 '20 at 12:56
  • Just figured this out right before I read the answer –  Jul 18 '20 at 13:00
  • Another option would be to put your `using` statements at the deepest scope (ie, within your namespace declaration). This would also help avoid other naming conflicts. Eg `namespace Console { using System; class Main {} }`. – Ctrl-Zed Mar 05 '21 at 11:17
  • Reverting @GlynnHurrell's recent edit since it was wrong. The original answer was correct. It wasn't looking for a _method_ called `WriteLine` it was looking for a type (just as the error says). – mjwills May 09 '21 at 23:21