Runtime: Namespace resolution not working

Created on 1 Jul 2017  路  7Comments  路  Source: dotnet/runtime

I created a simple Console app using .Net Core project template using Visual Studio and give that project name "Core.Console.CS1".

The default program.cs files looks like this

```c#
using System;

namespace Core.Console.CS1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
```

on building the project, i get the error: WriteLine not found in Core.Console namespace.

However, the full Namespace here is Core.Console.CS1, so my program should compile here.

area-Meta

Most helpful comment

Isn't this expected behavior all the way back to C# 1.0 on .NET Framework 1.0? You have to write System.Console because Console is in the class's namespace, or you have to use an alias like using Console = System.Console; or something.

I think that's also why the Xamarin templates call the project YourName.Droid, not YourName.Android, because that would make it a pain to access anything in the Xamarin.Android namespace. (Guess how I found out.)

All 7 comments

What is your exact VS version? Which .NET Core version do you use?
Does it happen to you on more than 1 machine? (This is very basic scenario which should just work everywhere - you might want to check if your machine is not corrupted)

Isn't this expected behavior all the way back to C# 1.0 on .NET Framework 1.0? You have to write System.Console because Console is in the class's namespace, or you have to use an alias like using Console = System.Console; or something.

I think that's also why the Xamarin templates call the project YourName.Droid, not YourName.Android, because that would make it a pain to access anything in the Xamarin.Android namespace. (Guess how I found out.)

Oh, I didn't notice the name collision of namespaces (I guess too early in the morning).
You are right. This is by design.

The namespace under which the Console.WriteLine is defined is not Core.Console, instead its Core.Console.CS1

Basically by design I am restricted to not to ReUse any variable or function name from Core or Core.Console , even if I am not referencing it directly.

When I use name space Core.Console.CS1, shouldn't it only check for naming duplication at Core.Console.CS1?

@patelchiraggit If you're inside the namespace Core.Console.CS1, it's sort of the same as if you had written:

c# using Core; using Core.Console; using Core.Console.CS1;

Others who are more familiar with the rules like @svick and @halofour can feel free to explain this better. =)

Got it. Thanks.

Was this page helpful?
0 / 5 - 0 ratings