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