Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. 'Item': member names cannot be the same as their enclosing type

'Item': member names cannot be the same as their enclosing type

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharphelp
29 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Redwan Albougha
    wrote on last edited by
    #1

    My schoolmate told me about a situation he faced while coding using C#. if we have the following code snippet :

    public class Item
    {
    public int this[int y]
    {
    get
    {
    return 0;
    }
    }
    }

    While compiling an error will appear saying: 'Item': member names cannot be the same as their enclosing type :wtf: First I got confused, since I knew that I was defining indexer property for the class 'Item'. After I referred to MSDN I read this : Compiler Error CS0542 If your class is named 'Item' and has an indexer declared as this, you may get this error. A default indexer is given the name 'Item' in the emitted code, creating the conflict. At the end I think that this MSIL specification mustn't be exist at all.:cool:

    Best wishes, Redwan Al-Bougha

    T P L C 4 Replies Last reply
    0
    • R Redwan Albougha

      My schoolmate told me about a situation he faced while coding using C#. if we have the following code snippet :

      public class Item
      {
      public int this[int y]
      {
      get
      {
      return 0;
      }
      }
      }

      While compiling an error will appear saying: 'Item': member names cannot be the same as their enclosing type :wtf: First I got confused, since I knew that I was defining indexer property for the class 'Item'. After I referred to MSDN I read this : Compiler Error CS0542 If your class is named 'Item' and has an indexer declared as this, you may get this error. A default indexer is given the name 'Item' in the emitted code, creating the conflict. At the end I think that this MSIL specification mustn't be exist at all.:cool:

      Best wishes, Redwan Al-Bougha

      T Offline
      T Offline
      Thomas Weller 0
      wrote on last edited by
      #2

      This specification exists because other .NET languages such as VB.NET have to explicitly write it down. Think of Item as just another reserved word in C#. Yes, MS could have chosen an ugly name like __MSIL$%Indexer%%__ or so, but what would it look like in VB or your documentation? Regards Thomas

      www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
      Programmer - an organism that turns coffee into software.

      R 1 Reply Last reply
      0
      • T Thomas Weller 0

        This specification exists because other .NET languages such as VB.NET have to explicitly write it down. Think of Item as just another reserved word in C#. Yes, MS could have chosen an ugly name like __MSIL$%Indexer%%__ or so, but what would it look like in VB or your documentation? Regards Thomas

        www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
        Programmer - an organism that turns coffee into software.

        R Offline
        R Offline
        Redwan Albougha
        wrote on last edited by
        #3

        Thomas Weller wrote:

        This specification exists because other .NET languages such as VB.NET have to explicitly write it down.

        That's right, I think if a wrapper exist for each language between it's syntax and MSIL emitted code, this will help a lot. ;)

        Best wishes, Redwan Al-Bougha

        T 1 Reply Last reply
        0
        • R Redwan Albougha

          Thomas Weller wrote:

          This specification exists because other .NET languages such as VB.NET have to explicitly write it down.

          That's right, I think if a wrapper exist for each language between it's syntax and MSIL emitted code, this will help a lot. ;)

          Best wishes, Redwan Al-Bougha

          T Offline
          T Offline
          Thomas Weller 0
          wrote on last edited by
          #4

          Redwan Al-Bougha wrote:

          I think if a wrapper exist for each language between it's syntax and MSIL emitted code

          :rolleyes: Well, such a 'wrapper' exists. They usually call it 'compiler'. Regards Thomas

          www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
          Programmer - an organism that turns coffee into software.

          R 1 Reply Last reply
          0
          • T Thomas Weller 0

            Redwan Al-Bougha wrote:

            I think if a wrapper exist for each language between it's syntax and MSIL emitted code

            :rolleyes: Well, such a 'wrapper' exists. They usually call it 'compiler'. Regards Thomas

            www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
            Programmer - an organism that turns coffee into software.

            R Offline
            R Offline
            Redwan Albougha
            wrote on last edited by
            #5

            Thomas Weller wrote:

            They usually call it 'compiler'.

            Thanks Thomas :) , I meant instead of : 1- Read language-specific syntax. 2- Convert code to MSIL code. Do this : 1- Read language-specific syntax, where each language is not related in any way to MSIL code. 2- Convert code to MSIL code. That's it avoid the happened ambiguity, as easy as this. Or easily don't use common -reserved- words in any .NET language. :-D

            Best wishes, Redwan Al-Bougha

            R 1 Reply Last reply
            0
            • R Redwan Albougha

              Thomas Weller wrote:

              They usually call it 'compiler'.

              Thanks Thomas :) , I meant instead of : 1- Read language-specific syntax. 2- Convert code to MSIL code. Do this : 1- Read language-specific syntax, where each language is not related in any way to MSIL code. 2- Convert code to MSIL code. That's it avoid the happened ambiguity, as easy as this. Or easily don't use common -reserved- words in any .NET language. :-D

              Best wishes, Redwan Al-Bougha

              R Offline
              R Offline
              Rob Graham
              wrote on last edited by
              #6

              You're missing the point. It's not that the MSIL code requires that the name of the property returned by the indexer be 'item', this is just a name that is auto generated by the compiler as a convenience. In your case, you have chosen the one and only class name that collides with the automatically generated name (and any name chosen for automatic generation will collide when used in a class of the same name). .Net also provides a convenient way to fix this: use an attribute to specify the name for the indexer so you can keep your class name as Item:

              public class Item
              {
              [System.Runtime.CompilerServices.IndexerName("TheItem")]
              public int this[int y]
              {
              get
              {
              return 0;
              }
              }
              }

              There is nothing to complain about here.

              R 1 Reply Last reply
              0
              • R Rob Graham

                You're missing the point. It's not that the MSIL code requires that the name of the property returned by the indexer be 'item', this is just a name that is auto generated by the compiler as a convenience. In your case, you have chosen the one and only class name that collides with the automatically generated name (and any name chosen for automatic generation will collide when used in a class of the same name). .Net also provides a convenient way to fix this: use an attribute to specify the name for the indexer so you can keep your class name as Item:

                public class Item
                {
                [System.Runtime.CompilerServices.IndexerName("TheItem")]
                public int this[int y]
                {
                get
                {
                return 0;
                }
                }
                }

                There is nothing to complain about here.

                R Offline
                R Offline
                Redwan Albougha
                wrote on last edited by
                #7

                Rob Graham wrote:

                this is just a name that is auto generated by the compiler as a convenience

                Thanks Rob, That's what I wrote in the 1st post :

                Redwan Al-Bougha wrote:

                A default indexer is given the name 'Item' in the emitted code, creating the conflict.

                By the way thanks for your notice and for IndexerName attribute:cool: Best Wishes, Redwan Al-Bougha

                1 Reply Last reply
                0
                • R Redwan Albougha

                  My schoolmate told me about a situation he faced while coding using C#. if we have the following code snippet :

                  public class Item
                  {
                  public int this[int y]
                  {
                  get
                  {
                  return 0;
                  }
                  }
                  }

                  While compiling an error will appear saying: 'Item': member names cannot be the same as their enclosing type :wtf: First I got confused, since I knew that I was defining indexer property for the class 'Item'. After I referred to MSDN I read this : Compiler Error CS0542 If your class is named 'Item' and has an indexer declared as this, you may get this error. A default indexer is given the name 'Item' in the emitted code, creating the conflict. At the end I think that this MSIL specification mustn't be exist at all.:cool:

                  Best wishes, Redwan Al-Bougha

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  This may more properly be in the Subtle Bugs forum, although it's not entirely subtle. Anyway, my complaint wouldn't be that the indexer is considered to be named "Item"; I dont like the "member names cannot be the same as their enclosing type" part. Why not? If I'm writing a Fibonacci method why shouldn't I put it in a class named Fibonacci? :confused:

                  P 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    This may more properly be in the Subtle Bugs forum, although it's not entirely subtle. Anyway, my complaint wouldn't be that the indexer is considered to be named "Item"; I dont like the "member names cannot be the same as their enclosing type" part. Why not? If I'm writing a Fibonacci method why shouldn't I put it in a class named Fibonacci? :confused:

                    P Offline
                    P Offline
                    Paul Conrad
                    wrote on last edited by
                    #9

                    PIEBALDconsult wrote:

                    Why not? If I'm writing a Fibonacci method why shouldn't I put it in a class named Fibonacci?

                    Exactly. Why not? If programming the class in C++, when you reference it you use something like Fibonacci::Fibonacci, which has always been clear to me what's going on.

                    "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

                    W 1 Reply Last reply
                    0
                    • P Paul Conrad

                      PIEBALDconsult wrote:

                      Why not? If I'm writing a Fibonacci method why shouldn't I put it in a class named Fibonacci?

                      Exactly. Why not? If programming the class in C++, when you reference it you use something like Fibonacci::Fibonacci, which has always been clear to me what's going on.

                      "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

                      W Offline
                      W Offline
                      WilliamSauron
                      wrote on last edited by
                      #10

                      In C# and C++ a method that has the same name as the class itself is called a "constructor"...

                      -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

                      P P 2 Replies Last reply
                      0
                      • W WilliamSauron

                        In C# and C++ a method that has the same name as the class itself is called a "constructor"...

                        -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

                        P Offline
                        P Offline
                        Paul Conrad
                        wrote on last edited by
                        #11

                        WilliamSauron wrote:

                        method that has the same name as the class itself is called a "constructor"...

                        Thanks, but I already know that. :-D

                        W 1 Reply Last reply
                        0
                        • P Paul Conrad

                          WilliamSauron wrote:

                          method that has the same name as the class itself is called a "constructor"...

                          Thanks, but I already know that. :-D

                          W Offline
                          W Offline
                          WilliamSauron
                          wrote on last edited by
                          #12

                          Yes, I actually hoped it was so :-) What I meant is that the limitation is somehow "logical". I don't find logical to have a class named "Fibonacci" that has a method named "Fibonacci". I would rather expect methods like "generate" or "next".

                          -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

                          P 1 Reply Last reply
                          0
                          • W WilliamSauron

                            Yes, I actually hoped it was so :-) What I meant is that the limitation is somehow "logical". I don't find logical to have a class named "Fibonacci" that has a method named "Fibonacci". I would rather expect methods like "generate" or "next".

                            -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

                            P Offline
                            P Offline
                            Paul Conrad
                            wrote on last edited by
                            #13

                            WilliamSauron wrote:

                            Yes, I actually hoped it was so

                            Yeah. With a Master's in CSCI, teaching CSCI at the college level, and over 15 years IT experience, one should know what a constructor is :-D

                            "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

                            1 Reply Last reply
                            0
                            • W WilliamSauron

                              In C# and C++ a method that has the same name as the class itself is called a "constructor"...

                              -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

                              P Offline
                              P Offline
                              PIEBALDconsult
                              wrote on last edited by
                              #14

                              No, at least in C#, a constructor is named .ctor:

                              System.DivideByZeroException: Attempted to divide by zero.
                              at Template.Template..ctor(Int32 x)

                              Like the indexer, the developer doesn't give it a name, only the return type and parameters.

                              W 1 Reply Last reply
                              0
                              • P PIEBALDconsult

                                No, at least in C#, a constructor is named .ctor:

                                System.DivideByZeroException: Attempted to divide by zero.
                                at Template.Template..ctor(Int32 x)

                                Like the indexer, the developer doesn't give it a name, only the return type and parameters.

                                W Offline
                                W Offline
                                WilliamSauron
                                wrote on last edited by
                                #15

                                Ok, it's time for a little nitpicking then :-) (or, as we say in French, cutting hairs in four). Beware, I've 44 years of experience at that game :-) It means that everything I say here has absolutely no importance at all, and is by no way an attack on anyone. It's just saying something for the pleasure of saying something. The ".ctor" is not a C# name, it is a CLR/MSIL/whatever name. When I write a constructor for my C# class, I dont type ".ctor", I type the name of my class as the name of the method. Now, whether the compiler has an urgent need to create MSIL code named ".ctor" or "Groborozgruduruk" is totally irrelevant. When I program in C#, and also when I read C# code written by anyone else, I know that if a method is declared that has the same name as the class, it is a constructor. In Delphi, type casts use the same syntax as a function call, so having a function and a type with the same name is not a good idea there neither. Other languages may still have other good reasons to abhor that. This is a good reason why I agree with the compiler when it insists that I don't call my method the same name as the class. This brings the second question: as the compiler internally creates a method named "Item" as the implementation of the default indexer, does it really impose an unbearable burden on the creativity of programmers? Of course, it means that you cannot have a class named "Item" that has an indexer. But wait! Do you really think a good design would involve a class named very generically "Item" that has a default indexer (and so is itself composed of a collection of something else.) What are these called then? "SubItem" maybe? What if SubItem has itself an indexer? "SubSubItem"? "YetAnoterSubItem"? If you don't give meaningful names to your classes and methods, you are of course welcome to do so, but please don't say the compiler is restricting your creativity :-) On the other hand, now we know where the coding horror is :-)

                                -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

                                P L R 3 Replies Last reply
                                0
                                • W WilliamSauron

                                  Ok, it's time for a little nitpicking then :-) (or, as we say in French, cutting hairs in four). Beware, I've 44 years of experience at that game :-) It means that everything I say here has absolutely no importance at all, and is by no way an attack on anyone. It's just saying something for the pleasure of saying something. The ".ctor" is not a C# name, it is a CLR/MSIL/whatever name. When I write a constructor for my C# class, I dont type ".ctor", I type the name of my class as the name of the method. Now, whether the compiler has an urgent need to create MSIL code named ".ctor" or "Groborozgruduruk" is totally irrelevant. When I program in C#, and also when I read C# code written by anyone else, I know that if a method is declared that has the same name as the class, it is a constructor. In Delphi, type casts use the same syntax as a function call, so having a function and a type with the same name is not a good idea there neither. Other languages may still have other good reasons to abhor that. This is a good reason why I agree with the compiler when it insists that I don't call my method the same name as the class. This brings the second question: as the compiler internally creates a method named "Item" as the implementation of the default indexer, does it really impose an unbearable burden on the creativity of programmers? Of course, it means that you cannot have a class named "Item" that has an indexer. But wait! Do you really think a good design would involve a class named very generically "Item" that has a default indexer (and so is itself composed of a collection of something else.) What are these called then? "SubItem" maybe? What if SubItem has itself an indexer? "SubSubItem"? "YetAnoterSubItem"? If you don't give meaningful names to your classes and methods, you are of course welcome to do so, but please don't say the compiler is restricting your creativity :-) On the other hand, now we know where the coding horror is :-)

                                  -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

                                  P Offline
                                  P Offline
                                  PIEBALDconsult
                                  wrote on last edited by
                                  #16

                                  WilliamSauron wrote:

                                  The ".ctor" is not a C# name, it is a CLR/MSIL/whatever name

                                  Which is the only name that matters.

                                  WilliamSauron wrote:

                                  I know that if a method is declared that has the same name as the class, it is a constructor.

                                  That's funny, I know that if a method is declared that has no name and returns an instance of the class, it is a constructor.

                                  W 1 Reply Last reply
                                  0
                                  • P PIEBALDconsult

                                    WilliamSauron wrote:

                                    The ".ctor" is not a C# name, it is a CLR/MSIL/whatever name

                                    Which is the only name that matters.

                                    WilliamSauron wrote:

                                    I know that if a method is declared that has the same name as the class, it is a constructor.

                                    That's funny, I know that if a method is declared that has no name and returns an instance of the class, it is a constructor.

                                    W Offline
                                    W Offline
                                    WilliamSauron
                                    wrote on last edited by
                                    #17

                                    PIEBALDconsult wrote:

                                    Which is the only name that matters

                                    Unless you target another architecture than the CLR, of course, in which case you are free to name generated symbols as you see fit. Who said that it is forbidden to directly generate IBM 360 assembly from C# ?

                                    PIEBALDconsult wrote:

                                    That's funny, I know that if a method is declared that has no name and returns an instance of the class, it is a constructor.

                                    Funnier, my copy of "The C# Programming Language", ISBN 0-321-15491-6 by Anders Hejlsberg, Scott Wiltamuth & Peter Golde, on chapter 10.10 "Instance Constructors", page 344 has strong hints that they understand the constructor name to be similar to a method name, not a type name:

                                    The identifier of a constructor-declarator must name the class in which the instance constructor is declared. If any other name is specified, a compile-time error occurs.

                                    Similarly, my Visual Studio highlights the constructor as a method name, not as a type. But hey, Visual Studio is a Microsoft product, these are known not to follow recognized standards. And who knows, maybe this Hejlsberg guy is just a newbie :-)

                                    -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

                                    P 1 Reply Last reply
                                    0
                                    • W WilliamSauron

                                      PIEBALDconsult wrote:

                                      Which is the only name that matters

                                      Unless you target another architecture than the CLR, of course, in which case you are free to name generated symbols as you see fit. Who said that it is forbidden to directly generate IBM 360 assembly from C# ?

                                      PIEBALDconsult wrote:

                                      That's funny, I know that if a method is declared that has no name and returns an instance of the class, it is a constructor.

                                      Funnier, my copy of "The C# Programming Language", ISBN 0-321-15491-6 by Anders Hejlsberg, Scott Wiltamuth & Peter Golde, on chapter 10.10 "Instance Constructors", page 344 has strong hints that they understand the constructor name to be similar to a method name, not a type name:

                                      The identifier of a constructor-declarator must name the class in which the instance constructor is declared. If any other name is specified, a compile-time error occurs.

                                      Similarly, my Visual Studio highlights the constructor as a method name, not as a type. But hey, Visual Studio is a Microsoft product, these are known not to follow recognized standards. And who knows, maybe this Hejlsberg guy is just a newbie :-)

                                      -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

                                      P Offline
                                      P Offline
                                      PIEBALDconsult
                                      wrote on last edited by
                                      #18

                                      WilliamSauron wrote:

                                      Unless you target another architecture than the CLR, of course

                                      True, but it would still likely generate the same name, for convenience. My copies of the MS and ECMA specs say the same thing... it's an identifier, not a name.

                                      W 1 Reply Last reply
                                      0
                                      • P PIEBALDconsult

                                        WilliamSauron wrote:

                                        Unless you target another architecture than the CLR, of course

                                        True, but it would still likely generate the same name, for convenience. My copies of the MS and ECMA specs say the same thing... it's an identifier, not a name.

                                        W Offline
                                        W Offline
                                        WilliamSauron
                                        wrote on last edited by
                                        #19

                                        PIEBALDconsult wrote:

                                        True, but it would still likely generate the same name, for convenience

                                        Yes, and since in some assembler languages (the one that comes to mind is Compass but there are others,) labels beginning with a dot must be local, that would make sure your constructors cannot be called from outside your module.

                                        PIEBALDconsult wrote:

                                        My copies of the MS and ECMA specs say the same thing... it's an identifier, not a name.

                                        Yes, if you prefer calling that an identifier instead of a name (let's not reach for the scissors and talk about the subtle semantic differences between a name and an identifier.) But still a method identifier, not a type identifier. Last time I looked, the identifier denoting a constructor in my Visual Studio editor had the same black color as another method identifier, not the greenish color of my class identifier. Are they all color blind at Microsoft?

                                        -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

                                        P 1 Reply Last reply
                                        0
                                        • W WilliamSauron

                                          PIEBALDconsult wrote:

                                          True, but it would still likely generate the same name, for convenience

                                          Yes, and since in some assembler languages (the one that comes to mind is Compass but there are others,) labels beginning with a dot must be local, that would make sure your constructors cannot be called from outside your module.

                                          PIEBALDconsult wrote:

                                          My copies of the MS and ECMA specs say the same thing... it's an identifier, not a name.

                                          Yes, if you prefer calling that an identifier instead of a name (let's not reach for the scissors and talk about the subtle semantic differences between a name and an identifier.) But still a method identifier, not a type identifier. Last time I looked, the identifier denoting a constructor in my Visual Studio editor had the same black color as another method identifier, not the greenish color of my class identifier. Are they all color blind at Microsoft?

                                          -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

                                          P Offline
                                          P Offline
                                          PIEBALDconsult
                                          wrote on last edited by
                                          #20

                                          WilliamSauron wrote:

                                          Visual Studio editor had the same black color

                                          Tools|Options|Environment|Fonts and Colors|Text Editor|Identifier VS doesn't discriminate between these identifiers, but I could write an editor that does. (I'll stick with Edit.) P.S. I finally got around to installing Leppie's xacc.ide -- it displays constructor identifiers in the greenish color.

                                          modified on Friday, November 28, 2008 1:07 PM

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups