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. Reference Types v. Value Types

Reference Types v. Value Types

Scheduled Pinned Locked Moved C#
csharpfunctionalquestionannouncement
8 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.
  • Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #1

    the .NET String class is a reference type, correct? Why then must I add the ref keyword to a string argument if I want its value to be updated in the called function? No other reference type behaves this way, correct? :confused: UPDATE: I just realized what's going on. Strings are immutable so when you pass a string with the ref keyword, it's the original reference to the string that is being updated with the new string. Without the ref keyword, the called function gets its own reference to the string, and if changes are made, then it's the function's reference that is updated, not the original reference. :)

    The difficult we do right away... ...the impossible takes slightly longer.

    OriginalGriffO B F 3 Replies Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      the .NET String class is a reference type, correct? Why then must I add the ref keyword to a string argument if I want its value to be updated in the called function? No other reference type behaves this way, correct? :confused: UPDATE: I just realized what's going on. Strings are immutable so when you pass a string with the ref keyword, it's the original reference to the string that is being updated with the new string. Without the ref keyword, the called function gets its own reference to the string, and if changes are made, then it's the function's reference that is updated, not the original reference. :)

      The difficult we do right away... ...the impossible takes slightly longer.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Dat's de bunny!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        the .NET String class is a reference type, correct? Why then must I add the ref keyword to a string argument if I want its value to be updated in the called function? No other reference type behaves this way, correct? :confused: UPDATE: I just realized what's going on. Strings are immutable so when you pass a string with the ref keyword, it's the original reference to the string that is being updated with the new string. Without the ref keyword, the called function gets its own reference to the string, and if changes are made, then it's the function's reference that is updated, not the original reference. :)

        The difficult we do right away... ...the impossible takes slightly longer.

        B Offline
        B Offline
        BillWoodruff
        wrote on last edited by
        #3

        I look at 'string as a chimera: a reference type (heap allocated [1], a class, no fixed allocation size, can be 'null) with value type semantics (immutability). Adding to the inter-species flavor is that == will compare content, not references. Under the hood a Char[]. [1] Eric Lippert's blogs illuminate the complexity of strings and memory allocation in .NET:

        Quote:

        "It is simply false that the choice of whether to use the stack or the heap has anything fundamentally to do with the type of the thing being stored. The truth is: the choice of allocation mechanism has to do only with the known required lifetime of the storage."

        [^] [^] you'll have to look for these since @@^77!! MSDN has archived them: http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation- detail.aspx http://blogs.msdn.com/b/ericlippert/archive/2009/05/04/the-stack-is-an-implementation- detail-part-two.aspx http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx

        «One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

        Richard Andrew x64R 1 Reply Last reply
        0
        • B BillWoodruff

          I look at 'string as a chimera: a reference type (heap allocated [1], a class, no fixed allocation size, can be 'null) with value type semantics (immutability). Adding to the inter-species flavor is that == will compare content, not references. Under the hood a Char[]. [1] Eric Lippert's blogs illuminate the complexity of strings and memory allocation in .NET:

          Quote:

          "It is simply false that the choice of whether to use the stack or the heap has anything fundamentally to do with the type of the thing being stored. The truth is: the choice of allocation mechanism has to do only with the known required lifetime of the storage."

          [^] [^] you'll have to look for these since @@^77!! MSDN has archived them: http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation- detail.aspx http://blogs.msdn.com/b/ericlippert/archive/2009/05/04/the-stack-is-an-implementation- detail-part-two.aspx http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx

          «One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

          Richard Andrew x64R Offline
          Richard Andrew x64R Offline
          Richard Andrew x64
          wrote on last edited by
          #4

          Thanks Bill! That's a very useful post.

          The difficult we do right away... ...the impossible takes slightly longer.

          B 1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            Thanks Bill! That's a very useful post.

            The difficult we do right away... ...the impossible takes slightly longer.

            B Offline
            B Offline
            BillWoodruff
            wrote on last edited by
            #5

            here's a Lippert blog I find very relevant: Strings, immutability and persistence[^] i like to confuse my students :) by asking them: if a Point is a ValueType (immutable), why can you set its X/Y properties directly without creating a new copy. To (maybe) get across the idea that immutable and passed by value are not necessarily synonymous.

            «One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

            1 Reply Last reply
            0
            • Richard Andrew x64R Richard Andrew x64

              the .NET String class is a reference type, correct? Why then must I add the ref keyword to a string argument if I want its value to be updated in the called function? No other reference type behaves this way, correct? :confused: UPDATE: I just realized what's going on. Strings are immutable so when you pass a string with the ref keyword, it's the original reference to the string that is being updated with the new string. Without the ref keyword, the called function gets its own reference to the string, and if changes are made, then it's the function's reference that is updated, not the original reference. :)

              The difficult we do right away... ...the impossible takes slightly longer.

              F Offline
              F Offline
              F ES Sitecore
              wrote on last edited by
              #6

              Richard Andrew x64 wrote:

              the .NET String class is a reference type, correct?

              No, it's a value type. Even though strings are heap-based, .net does smoke-and-mirrors behind the scenes to ensure strings behave like value types. Probably to make the language easier to use as strings are very common. The downside is that it can lead to poor performance if you're not aware of the caveats.

              R 1 Reply Last reply
              0
              • F F ES Sitecore

                Richard Andrew x64 wrote:

                the .NET String class is a reference type, correct?

                No, it's a value type. Even though strings are heap-based, .net does smoke-and-mirrors behind the scenes to ensure strings behave like value types. Probably to make the language easier to use as strings are very common. The downside is that it can lead to poor performance if you're not aware of the caveats.

                R Offline
                R Offline
                Richard Deeming
                wrote on last edited by
                #7

                F-ES Sitecore wrote:

                No, it's a value type.

                No, it's an immutable reference type. There's not much in the way of smoke-and-mirrors involved; you can easily create your own immutable reference type to get the same behaviour. If string was really a value type, you wouldn't sensibly be able to have a value type with a string member. The entire string would be stored "in-line" within that struct, and you'd end up with stack overflow exceptions everywhere. It's probably better to say that in some circumstances it's easier to think of string as a value type, even though it isn't. :)


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                F 1 Reply Last reply
                0
                • R Richard Deeming

                  F-ES Sitecore wrote:

                  No, it's a value type.

                  No, it's an immutable reference type. There's not much in the way of smoke-and-mirrors involved; you can easily create your own immutable reference type to get the same behaviour. If string was really a value type, you wouldn't sensibly be able to have a value type with a string member. The entire string would be stored "in-line" within that struct, and you'd end up with stack overflow exceptions everywhere. It's probably better to say that in some circumstances it's easier to think of string as a value type, even though it isn't. :)


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  F Offline
                  F Offline
                  F ES Sitecore
                  wrote on last edited by
                  #8

                  Richard Deeming wrote:

                  it's easier to think of string as a value type, even though it isn't

                  Which is what I was saying.

                  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