I suspect this is a question that has been asked many times, but I have not found it.
I usually use fully qualified namespaces unless I often use this type in a file or add using namaspacename to the top of the file to write new ClassName() .
But what if only part of the complete namespace were added? Why can't the compiler find the type and throw an error?
Consider the following class in a nested namespace:
namespace ns_1 { namespace ns_1_1 { public class Foo { } } }
So, if now I want to initialize an instance of this class, it works as follows:
using ns_1.ns_1_1; public class Program { public Program() {
But the following does not work:
using ns_1; public class Program { public Program() {
it throws a compiler error:
Could not find the type or namespace name 'ns_1_1' (are you missing the using directive or assembly reference?)
I assume that since ns_1_1 seen as a class that contains a different class Foo instead of a namespace, is this correct?
I did not find the language specification, where is this documented? Why is the compiler not smart enough to check if there is a class namespace or (-part)?
Here's another - less abstract - example of what I mean:
using System.Data; public class Program { public Program() { using (var con = new SqlClient.SqlConnection("..."))
Edit : Now I know why this seems very strange to me. It works without problems in VB.NET:
Imports System.Data Public Class Program Public Sub New() Using con = New SqlClient.SqlConnection("...") ' no problem End Using End Sub End Class