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. General Programming
  3. C#
  4. Covariant return types?

Covariant return types?

Scheduled Pinned Locked Moved C#
csharpc++comtoolshelp
14 Posts 5 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.
  • S S Senthil Kumar

    Because users of StringItem would have to cast to a string. They already know they have a StringItem instance, so why should they have to cast the return value of GetContents to string again? In my code, the user would first have to cast from GeneralItem to StringItem anyway. So he/she actually ends up casting twice. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

    N Offline
    N Offline
    Niklas Ulvinge
    wrote on last edited by
    #4

    I think you have to live with that... The PROgrammer Niklas Ulvinge aka IDK

    S 1 Reply Last reply
    0
    • N Niklas Ulvinge

      I think you have to live with that... The PROgrammer Niklas Ulvinge aka IDK

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #5

      I want to know why there are no covariant return types, I do understand that I've to live with it, (atleast until the next version of C# rolls out, whenever that is.) Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      N 1 Reply Last reply
      0
      • S S Senthil Kumar

        I want to know why there are no covariant return types, I do understand that I've to live with it, (atleast until the next version of C# rolls out, whenever that is.) Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        N Offline
        N Offline
        Niklas Ulvinge
        wrote on last edited by
        #6

        Becouse if you use that func and think you got the parent, but got the child and it return a string when you wanted an object. PS. They could do it with only child's to the return type is able to be a return type (and for object, that's everything...) The PROgrammer Niklas Ulvinge aka IDK

        S 1 Reply Last reply
        0
        • S S Senthil Kumar

          Does someone know why C# doesn't have covariant return types? All I want to do is this

          class GeneralItem
          {
          public abstract object GetContents();
          }

          class StringItem : GeneralItem
          {
          public override string GetContents()
          {...}
          }

          I get a compiler error saying it doesn't like return types of overridden methods to be different. It's legal in C++, so why is it not available in C#? The workaround suggested is to provide an explicit interface implementation, but I don't have one defined already. Defining a new interface just doesn't seem right. It isn't available in C# 2.0 either. Any idea why? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #7

          Probably because Microsoft thought it would be confusing. Why would you want to do this ? If the overridden classes differ by return type, how will you know what you're getting back anyhow ? Christian Graus - Microsoft MVP - C++

          S 1 Reply Last reply
          0
          • C Christian Graus

            Probably because Microsoft thought it would be confusing. Why would you want to do this ? If the overridden classes differ by return type, how will you know what you're getting back anyhow ? Christian Graus - Microsoft MVP - C++

            S Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #8

            I'm anyway going to get a string if I call GetContents, whether it is on GeneralItem (with runtime type = StringItem) or StringItem directly. For StringItem, I already know I'll get a string, so why do I have to cast again? For example

            // Assuming item is a StringItem at runtime.
            private void ProcessItem(GeneralItem item)
            {
            Console.WriteLine(item.GetContents()); // Perfectly okay.
            }

            private void ProcessItem(StringItem item)
            {
            string s = (string)item.GetContents(); // Why cast to string?
            }

            Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            F 1 Reply Last reply
            0
            • N Niklas Ulvinge

              Becouse if you use that func and think you got the parent, but got the child and it return a string when you wanted an object. PS. They could do it with only child's to the return type is able to be a return type (and for object, that's everything...) The PROgrammer Niklas Ulvinge aka IDK

              S Offline
              S Offline
              S Senthil Kumar
              wrote on last edited by
              #9

              It shouldn't be a problem, after all, a string "is-a" object, so the code that calls GetContents() on GeneralItem should be able to handle any type whose base class is object. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

              1 Reply Last reply
              0
              • S S Senthil Kumar

                I'm anyway going to get a string if I call GetContents, whether it is on GeneralItem (with runtime type = StringItem) or StringItem directly. For StringItem, I already know I'll get a string, so why do I have to cast again? For example

                // Assuming item is a StringItem at runtime.
                private void ProcessItem(GeneralItem item)
                {
                Console.WriteLine(item.GetContents()); // Perfectly okay.
                }

                private void ProcessItem(StringItem item)
                {
                string s = (string)item.GetContents(); // Why cast to string?
                }

                Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                F Offline
                F Offline
                FriendlyFiend
                wrote on last edited by
                #10

                You are thinking only in terms of string is a typeof object kind of way what, if the return types did not come from the same hierarchy at all eg. base class returns point your override returns string, this is a very difficult thing to keep track of especially if you want the compiler to throw errors in these kind of situations ...

                S 1 Reply Last reply
                0
                • F FriendlyFiend

                  You are thinking only in terms of string is a typeof object kind of way what, if the return types did not come from the same hierarchy at all eg. base class returns point your override returns string, this is a very difficult thing to keep track of especially if you want the compiler to throw errors in these kind of situations ...

                  S Offline
                  S Offline
                  S Senthil Kumar
                  wrote on last edited by
                  #11

                  The compiler doesn't allow that. If the overridding method's return type is different, it must be a derived class of the base class method's return type. So your case, (base class method returning Point, derived class method returning string) will not compile. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                  1 Reply Last reply
                  0
                  • S S Senthil Kumar

                    Does someone know why C# doesn't have covariant return types? All I want to do is this

                    class GeneralItem
                    {
                    public abstract object GetContents();
                    }

                    class StringItem : GeneralItem
                    {
                    public override string GetContents()
                    {...}
                    }

                    I get a compiler error saying it doesn't like return types of overridden methods to be different. It's legal in C++, so why is it not available in C#? The workaround suggested is to provide an explicit interface implementation, but I don't have one defined already. Defining a new interface just doesn't seem right. It isn't available in C# 2.0 either. Any idea why? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                    K Offline
                    K Offline
                    Kevin McFarlane
                    wrote on last edited by
                    #12

                    There's some discussion here: http://blogs.msdn.com/cyrusn/archive/2004/12/08/278661.aspx[^] Kevin

                    S 1 Reply Last reply
                    0
                    • K Kevin McFarlane

                      There's some discussion here: http://blogs.msdn.com/cyrusn/archive/2004/12/08/278661.aspx[^] Kevin

                      S Offline
                      S Offline
                      S Senthil Kumar
                      wrote on last edited by
                      #13

                      Thanks. Yes, I did read that but it doesn't seem to answer why covariant return types are not available in C#. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                      K 1 Reply Last reply
                      0
                      • S S Senthil Kumar

                        Thanks. Yes, I did read that but it doesn't seem to answer why covariant return types are not available in C#. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                        K Offline
                        K Offline
                        Kevin McFarlane
                        wrote on last edited by
                        #14

                        There doesn't seem to be an answer but there's yet more discussion here. http://www.gotdotnet.com/Community/MessageBoard/Thread.aspx?id=308523[^] It looks like it's not in C# 2.0 either but they are supported with delegates. http://msdn2.microsoft.com/library/ms173174(en-us,vs.80).aspx[^] C++/CLI also doesn't support them. It should be possible to do this kind of thing though, given sufficient desire, because Eiffel .NET was able to map all its language features (including covariant return types, generics and multiple inheritance) onto .NET 1.1. Kevin

                        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