[BUG] Compile can't resolve namespace class differences

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt Mastracci

    [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

  • Christian

    #2
    Re: [BUG] Compile can't resolve namespace class differences

    Classes can not have same name of namespaces.
    Christian.
    "Matt Mastracci" <matt@aclaro.co m> ha scritto nel messaggio
    news:%23FjA9ffU DHA.2224@TK2MSF TNGP10.phx.gbl. ..[color=blue]
    > 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'[/color]
    was[color=blue]
    > expected
    >[/color]


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: [BUG] Compile can't resolve namespace class differences

      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]
      - nicholas.paldin o@exisconsultin g.com

      "Matt Mastracci" <matt@aclaro.co m> wrote in message
      news:%23FjA9ffU DHA.2224@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > 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'[/color]
      was[color=blue]
      > expected
      >[/color]


      Comment

      • Matt Mastracci

        #4
        Re: [BUG] Compile can't resolve namespace class differences

        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:
        [color=blue]
        > Of course.
        > Doesn'it?
        > "Matt Mastracci" <matt@aclaro.co m> ha scritto nel messaggio
        > news:3F1FFD4E.1 070300@aclaro.c om...
        >[color=green]
        >>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:
        >>
        >>[color=darkred]
        >>>Matt,
        >>>
        >>> It is not unambiguous, and it is actually disallowed. You can not[/color][/color]
        >
        > have
        >[color=green][color=darkred]
        >>>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[/color][/color]
        >
        > compiler
        >[color=green][color=darkred]
        >>>would have to guess without knowing what C is, and I wouldn't put my[/color][/color]
        >
        > trust
        >[color=green][color=darkred]
        >>>in any compiler that did that.
        >>>
        >>> Hope this helps.
        >>>[/color]
        >>[/color]
        >
        >[/color]

        Comment

        • Matt Mastracci

          #5
          Re: [BUG] Compile can't resolve namespace class differences

          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:
          [color=blue]
          > Of course.
          > Doesn'it?
          > "Matt Mastracci" <matt@aclaro.co m> ha scritto nel messaggio
          > news:3F1FFD4E.1 070300@aclaro.c om...
          >[color=green]
          >>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:
          >>
          >>[color=darkred]
          >>>Matt,
          >>>
          >>> It is not unambiguous, and it is actually disallowed. You can not[/color][/color]
          >
          > have
          >[color=green][color=darkred]
          >>>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[/color][/color]
          >
          > compiler
          >[color=green][color=darkred]
          >>>would have to guess without knowing what C is, and I wouldn't put my[/color][/color]
          >
          > trust
          >[color=green][color=darkred]
          >>>in any compiler that did that.
          >>>
          >>> Hope this helps.
          >>>[/color]
          >>[/color]
          >
          >[/color]

          Comment

          • Matt Mastracci

            #6
            Re: [BUG] Compile can't resolve namespace class differences

            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:[color=blue]
            > 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.
            >[/color]

            Comment

            • Grant Richins [MS]

              #7
              Re: [BUG] Compile can't resolve namespace class differences

              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" <matt@aclaro.co m> wrote in message
              news:3F20053B.9 010506@aclaro.c om...[color=blue]
              > 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:
              >[color=green]
              > > Of course.
              > > Doesn'it?
              > > "Matt Mastracci" <matt@aclaro.co m> ha scritto nel messaggio
              > > news:3F1FFD4E.1 070300@aclaro.c om...
              > >[color=darkred]
              > >>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[/color]
              > >
              > > have
              > >[color=darkred]
              > >>>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[/color]
              > >
              > > compiler
              > >[color=darkred]
              > >>>would have to guess without knowing what C is, and I wouldn't put my[/color]
              > >
              > > trust
              > >[color=darkred]
              > >>>in any compiler that did that.
              > >>>
              > >>> Hope this helps.
              > >>>
              > >>[/color]
              > >
              > >[/color]
              >[/color]


              Comment

              • Grant Richins [MS]

                #8
                Re: [BUG] Compile can't resolve namespace class differences

                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" <pleasenononosp amcmillotti@nov asoftware.it> wrote in message
                news:eirXyifUDH A.1872@TK2MSFTN GP12.phx.gbl...[color=blue]
                > Classes can not have same name of namespaces.
                > Christian.
                > "Matt Mastracci" <matt@aclaro.co m> ha scritto nel messaggio
                > news:%23FjA9ffU DHA.2224@TK2MSF TNGP10.phx.gbl. ..[color=green]
                > > 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'[/color]
                > was[color=green]
                > > expected
                > >[/color]
                >
                >[/color]


                Comment

                Working...