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. Why string is reference data type.

Why string is reference data type.

Scheduled Pinned Locked Moved C#
csharp
4 Posts 4 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.
  • U Offline
    U Offline
    User 1804931
    wrote on last edited by
    #1

    In .Net why is string is treated as reference type, it is a primitive datatype. All premitive datatype are value type except string why this is. Is there any spl reason. Regards, Chakravarthy.v

    S C V 3 Replies Last reply
    0
    • U User 1804931

      In .Net why is string is treated as reference type, it is a primitive datatype. All premitive datatype are value type except string why this is. Is there any spl reason. Regards, Chakravarthy.v

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

      I'd say it's because strings can get very large. The maximum size of other primitive data types is predefined, but strings can grow to arbitrary lengths. You certainly wouldn't want to copy a 100,000 characters long string everytime you pass it to a function (by default) or put it in a collection (boxing). Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      1 Reply Last reply
      0
      • U User 1804931

        In .Net why is string is treated as reference type, it is a primitive datatype. All premitive datatype are value type except string why this is. Is there any spl reason. Regards, Chakravarthy.v

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        chakravarthy_vc wrote: In .Net why is string is treated as reference type, it is a primitive datatype. I wouldn't regard a string as a primitive datatype. I would say that a string is an array of primitive datatypes (i.e. char)


        My: Blog | Photos | Next SQL Presentation WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

        1 Reply Last reply
        0
        • U User 1804931

          In .Net why is string is treated as reference type, it is a primitive datatype. All premitive datatype are value type except string why this is. Is there any spl reason. Regards, Chakravarthy.v

          V Offline
          V Offline
          vims
          wrote on last edited by
          #4

          System.String is a reference type and not a value type, which means each string is allocated on the heap and not the stack. System.String is peculiar in that it has a shortcut keyword associated with it (string), which is a common practice with primitive value types - System.Int32 (int), System.Char (char) etc. String references also do not require the new keyword to allocate an instance, the following line of code will allocate space on the heap, fill it with string data and assign the reference to the variable: A string is a sequential collection of Unicode characters, typically used to represent text, while a String is a sequential collection of System.Char objects that represents a string. The value of the String is the content of the sequential collection, and the value is immutable. The ANSI string class implements a first-class character string data type that avoids many problems associated with simple character arrays vimsy

          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