Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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;
       }

   }

C#
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
Updated 13-Feb-12 1:44am
v2
Comments
Rajesh Anuhya 13-Feb-12 7:45am    
Edited: Code Tags added.
--RA

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.

C#
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";
        }

    }
}
 
Share this answer
 
v2
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
 
Share this answer
 
v2
Comments
Andreas Gieriet 15-Feb-12 8:07am    
My 5 for the more detailed description than my simplifying one in solution #4!
Sergey Alexandrovich Kryukov 15-Feb-12 11:09am    
Thank you, Andreas.
--SA
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!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 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
 
Share this answer
 
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
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900