Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: string
I Searched in many websites for solution,but i am not satisfied with that answers.
 
Please clarify my doubt.
 

Why integer is valutype and why string is Reference type.
 
I know theoretically,int is stored in stack and string is store in heap.but i did not have clarity.
 
For example:
 
int i=10;
int j=i;
i=12;
console.writeline(i);
console.writeline(j);
 

O/P;
i=12;j=10;
 
similarly
 
string str1="Hello";
string str2=str1;
 str1="World";
            System.Console.WriteLine(str1);
            System.Console.WriteLine(str2);
 
O/p:
str1=World;
str2=Hello;
 
Both integer and string are same right...
 
class AddPrg
   {
       public String Name
       {
           get;
           set;
       }
 
   }
class CPrograms
   {
 

       static void Main()
       {
 
 AddPrg obg = new AddPrg();
             obg.Name = "Hello";
             AddPrg obg1 = obg;
             obg1.Name = "World";
             System.Console.WriteLine(obg1.Name);
             System.Console.WriteLine(obg.Name);
 

}}
 
O/P is World,World both are same.so that class is reference type.
 

 
Hope you understand my question.Please clarify my doubt.. Thanks in Advance...
Posted 13-Feb-12 2:18am
Edited 13-Feb-12 2:44am
v2
Comments
Rajesh Anuhya at 13-Feb-12 7:45am
   
Edited: Code Tags added.
--RA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I m giving this answer by reference to JAVA:
 
integer is basic datatype while string is not.
 
int is treated like single component.
 
While string is an object of a class name String
 
when u write
int a;
a refer to the memory location where int number is stored
while when u write
String a;
a is not string object. a fefer to an reference of string object which contain original data
 
in java a is of type I m giving this answer by reference to JAVA:
 
integer is basic datatype while string is not.
 
int is treated like single component.
 
While string is an object of a class name String
 

when u write
int a;
a refer to the memory location where int number is stored
while when u write
String a;
a is not string object. a fefer to an reference of string object which contain original data
 
in JAVA a will be of class type not String.a will point to object of type String
 
Check this exapmle updating a will also update b which is possible in reference type not in valutype.
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            myString a=new myString();
            myString b = a;
            a.Name = "Hello";
            Console.Write(a.Name);
            Console.Write(b.Name);
 
            a.change();
 
            Console.Write(a.Name);
            Console.Write(b.Name);
        }
    }
    public class myString
    {
        public String Name
        {
            get;
            set;
        }
        public void change()
        {
            Name="World";
        }
 
    }
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

This is not so simple. First of all, value type can be stored 1) in stack; 2) in data segment reserved for static data, 3) on heap is this is a member of reference type.
 
As to reference types, it is also not so simple. References are something like pointers (but very different, due to managed (Garbage Collector based) architecture). There are always two memory objects involved: a reference itself and the referenced object. The referenced object can be stored only in heap, but the reference itself — anywhere, exactly like a value object.
 
In C#, you cannot access reference and reference object separately or explicitly, but this is possible in C++/CLI.
 
You should not also forget about boxing, see http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx[^].
 
Now, string class is very special. This is a reference type thoroughly simulating value semantic. Its functionality is bases on string pool and interning, pleas see:
http://en.wikipedia.org/wiki/String_interning[^]
 
For further detail, please see my past answers:
http://java.codeproject.com/Answers/153650/String-a-referance-type-variable-creating-confussi#answer2[^],
Is string a value type or Reference type..[^],
Difference Between Mutable , Immutable string in C#[^].
 
—SA
  Permalink  
v2
Comments
Andreas Gieriet at 15-Feb-12 8:07am
   
My 5 for the more detailed description than my simplifying one in solution #4!
SAKryukov at 15-Feb-12 11:09am
   
Thank you, Andreas.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Int is a value type because it is a very simple type of data: fixed length, small, with easy to handle operations.
String on the other hand is a lot more complex. It is of variable length (up to 2 Gb!) with complicated operations. It is also immutable, which is not practical with stack based data.
 
It would not be reasonable to put a string on the stack: the size alone could make it blow the stack to pieces!
  Permalink  
Comments
SAKryukov at 13-Feb-12 16:53pm
   
Griff, string is special not just because of that.
 
It simulates value semantic, uses string pool and string interning.
Please see my answer.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

The simple but maybe not so helpful answer to your question
 
Why integer is valutype and why string is Reference type.
 
is probably since the C#/.Net Framework designers felt it is a good decision.
 
From a pure technical point of view, it is not a must for a language to make that distinction between value and reference type. E.g. C++ does not know that distinction.
 
C#/.Net have the concept of value types and reference types. The value type has an intrinsic value semantic (with some technical and logical consequences like the one that value types objects are copied if used as method argument).
 
The references types are those that are not copied when passed as argument to the method - only a reference to that object is passed to the method, not the object itself.
This has also technical and logical implications, like the one that a referenced object can be modified inside the method so that the modification has effect outside the method.
 
Referencing intead of copying an object may be a large performance improvement.
 
Since strings are often passed to methods, the designes of the C#/.Net have decided to make string a class, i.e. a reference type. On the other hand, the possibility to modify the string inside a method is generally assumed to be an undesired effect. That's why the string class is implemented such that it behaves like a value type.
 
Maybe this explanation is a bit over simplifying some facts, but I think you get the general imression on the topic.
 
Cheers
 
Andi
  Permalink  
Comments
SAKryukov at 13-Feb-12 16:55pm
   
Andi, this is not that simple.
 
Especially with the strings.
This type simulates value semantic, uses string pool and string interning. Please see my answer.
--SA
Andreas Gieriet at 15-Feb-12 8:02am
   
Privet SAKryukov,
 
I assume that the OP was interested in a rather high level answer. Interning etc. is rather an implementation detail of an imutable string type, basically optimising memory management.
 
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 13 Feb 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100