473,657 Members | 2,430 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[BUG] Compile can't resolve namespace class differences

It seems like the C# compile can't resolve the difference between a
namespace and a class in a situation that is clearly unambiguous. Note
that it sees the namespace "C" before the class "C". I believe it
should see the class "C" first.

test1.cs:
=============

using System;
using A.C;

namespace A.B
{
public class D
{
D() { C c = new C(); }
}
}

test2.cs:
=============

using System;

namespace A.C
{
public class C
{
int X;
}
}

=============

C:\>csc /target:library test1.cs test2.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.

test1.cs(8,9): error CS0118: 'A.C' denotes a 'namespace' where a 'class' was
expected

Nov 15 '05 #1
7 3533
Classes can not have same name of namespaces.
Christian.
"Matt Mastracci" <ma**@aclaro.co m> ha scritto nel messaggio
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
It seems like the C# compile can't resolve the difference between a
namespace and a class in a situation that is clearly unambiguous. Note
that it sees the namespace "C" before the class "C". I believe it
should see the class "C" first.

test1.cs:
=============

using System;
using A.C;

namespace A.B
{
public class D
{
D() { C c = new C(); }
}
}

test2.cs:
=============

using System;

namespace A.C
{
public class C
{
int X;
}
}

=============

C:\>csc /target:library test1.cs test2.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.

test1.cs(8,9): error CS0118: 'A.C' denotes a 'namespace' where a 'class' was expected

Nov 15 '05 #2
Matt,

It is not unambiguous, and it is actually disallowed. You can not have
a class in a namespace with the same name as the namespace explicitly
because of this. What if you had a nested type in class C? The compiler
would have to guess without knowing what C is, and I wouldn't put my trust
in any compiler that did that.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"Matt Mastracci" <ma**@aclaro.co m> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
It seems like the C# compile can't resolve the difference between a
namespace and a class in a situation that is clearly unambiguous. Note
that it sees the namespace "C" before the class "C". I believe it
should see the class "C" first.

test1.cs:
=============

using System;
using A.C;

namespace A.B
{
public class D
{
D() { C c = new C(); }
}
}

test2.cs:
=============

using System;

namespace A.C
{
public class C
{
int X;
}
}

=============

C:\>csc /target:library test1.cs test2.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.

test1.cs(8,9): error CS0118: 'A.C' denotes a 'namespace' where a 'class' was expected

Nov 15 '05 #3
Nope... Give it a shot. :)

BTW, after some thought, I think it's only ambiguous if you have both a
namespace "C" and a class "C" off the namespace "A.B". If I have a
class C with an inner class C in namespace C, the compiler can throw the
ambiguous reference error and I have:

A.B.C.C <- outer class
A.B.C.C.C <- inner class

If I have class C (1) with inner class C in namespace A.B and a class C
(2) in namespace A.B.C, it gets ambiguous:

A.B.C.C <- C (1)
A.B.C.C <- C (2)

Matt.

Christian wrote:
Of course.
Doesn'it?
"Matt Mastracci" <ma**@aclaro.co m> ha scritto nel messaggio
news:3F******** ******@aclaro.c om...
Should the C# compiler then be marking the class with the same name as
the namespace as an error?

Nicholas Paldino [.NET/C# MVP] wrote:

Matt,

It is not unambiguous, and it is actually disallowed. You can not
have
a class in a namespace with the same name as the namespace explicitly
because of this. What if you had a nested type in class C? The
compiler
would have to guess without knowing what C is, and I wouldn't put my
trust
in any compiler that did that.

Hope this helps.



Nov 15 '05 #4
Here's an example of more buggy (IMHO, of course) behaviour. Note that
in this case, a namespace higher the "current" namespace is interfering
with a reference from yet another namespace.

test1.cs
===========

using System;
using A.B;

namespace NA.NB.NC.ND
{
public class XXX
{
XXX() { Configuration c = new Configuration() ; }
}
}
namespace NA.Configuratio n
{
}

test2.cs
===========

using System;

namespace A.B
{
public class Configuration
{
public Configuration() { X = 3;}
public int X;
}
}

Christian wrote:
Of course.
Doesn'it?
"Matt Mastracci" <ma**@aclaro.co m> ha scritto nel messaggio
news:3F******** ******@aclaro.c om...
Should the C# compiler then be marking the class with the same name as
the namespace as an error?

Nicholas Paldino [.NET/C# MVP] wrote:

Matt,

It is not unambiguous, and it is actually disallowed. You can not
have
a class in a namespace with the same name as the namespace explicitly
because of this. What if you had a nested type in class C? The
compiler
would have to guess without knowing what C is, and I wouldn't put my
trust
in any compiler that did that.

Hope this helps.



Nov 15 '05 #5
What about this case. This looks like the ambiguous "nested class"
situation you've mentioned, but compiles fine. Note that the output of
this program is "NA.Configurati on.Inner".

test1.cs
=============

using System;
using A.B;

namespace NA.NB.NC.ND
{
public class XXX
{
static void Main() { Configuration.I nner c = new
Configuration.I nner(); c.Write(); }
}
}
namespace NA.Configuratio n
{
public class Inner
{
public void Write() { Console.WriteLi ne( this.GetType(). FullName ); }
}
}

test2.cs
============

using System;

namespace A.B
{
public class Configuration
{
public Configuration() { X = 3;}
public int X;

public class Inner
{
public void Write() { Console.WriteLi ne(
this.GetType(). FullName ); }
}
}
}

Nicholas Paldino [.NET/C# MVP] wrote:
Matt,

It is not unambiguous, and it is actually disallowed. You can not have
a class in a namespace with the same name as the namespace explicitly
because of this. What if you had a nested type in class C? The compiler
would have to guess without knowing what C is, and I wouldn't put my trust
in any compiler that did that.

Hope this helps.


Nov 15 '05 #6
Neither of these are ambiguous. The language spec is very clear about
things that make this unambiguous. When doing name resolution namespaces
are searched before using clauses (and outer namespaces with outer using
clauses). Thus in this example namespace NA is searched for a name
"Configurat ion" before even looking at your outermost using clauses.
Secondly name resolution occurs without consideration for the context of
where the name is used. In your example it is obvious that you expect
Configuration to be a type-name, but name lookup does not include that
information, it simply looks for the first "Configuration" , it is then an
error if the name that it finds is not used properly. The exact same
reasons apply to your original example.

--
--Grant
This posting is provided "AS IS" with no warranties, and confers no rights.
"Matt Mastracci" <ma**@aclaro.co m> wrote in message
news:3F******** ******@aclaro.c om...
Here's an example of more buggy (IMHO, of course) behaviour. Note that
in this case, a namespace higher the "current" namespace is interfering
with a reference from yet another namespace.

test1.cs
===========

using System;
using A.B;

namespace NA.NB.NC.ND
{
public class XXX
{
XXX() { Configuration c = new Configuration() ; }
}
}
namespace NA.Configuratio n
{
}

test2.cs
===========

using System;

namespace A.B
{
public class Configuration
{
public Configuration() { X = 3;}
public int X;
}
}

Christian wrote:
Of course.
Doesn'it?
"Matt Mastracci" <ma**@aclaro.co m> ha scritto nel messaggio
news:3F******** ******@aclaro.c om...
Should the C# compiler then be marking the class with the same name as
the namespace as an error?

Nicholas Paldino [.NET/C# MVP] wrote:
Matt,

It is not unambiguous, and it is actually disallowed. You can not


have
a class in a namespace with the same name as the namespace explicitly
because of this. What if you had a nested type in class C? The


compiler
would have to guess without knowing what C is, and I wouldn't put my


trust
in any compiler that did that.

Hope this helps.


Nov 15 '05 #7
Not entirely true. Types SHOULD not have the same name as their enclosing
namespace. The language does allow them to be the same, but then callers of
the class have to be careful when writing code to make sure they always bind
to the class and not the namespace.

It is true that inner types cannot have the same name as their outer type.

--
--Grant
This posting is provided "AS IS" with no warranties, and confers no rights.
"Christian" <pl************ ***********@nov asoftware.it> wrote in message
news:ei******** ******@TK2MSFTN GP12.phx.gbl...
Classes can not have same name of namespaces.
Christian.
"Matt Mastracci" <ma**@aclaro.co m> ha scritto nel messaggio
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
It seems like the C# compile can't resolve the difference between a
namespace and a class in a situation that is clearly unambiguous. Note
that it sees the namespace "C" before the class "C". I believe it
should see the class "C" first.

test1.cs:
=============

using System;
using A.C;

namespace A.B
{
public class D
{
D() { C c = new C(); }
}
}

test2.cs:
=============

using System;

namespace A.C
{
public class C
{
int X;
}
}

=============

C:\>csc /target:library test1.cs test2.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.

test1.cs(8,9): error CS0118: 'A.C' denotes a 'namespace' where a 'class'

was
expected


Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2308
by: Matt Mastracci | last post by:
It seems like the C# compile can't resolve the difference between a namespace and a class in a situation that is clearly unambiguous. Note that it sees the namespace "C" before the class "C". I believe it should see the class "C" first. test1.cs: ============= using System; using A.C;
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8726
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.