Loading ...

Value Types and Reference Types

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » Value Types and Reference Types

Value Types and Reference Types

Posts under the topic: Value Types and Reference Types

Posted: 10/1/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Hello Folks,

I have read a blog on codeasp and modify the code -

Here is blog's code :-

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        
        static void Main(string[] args)
        {
            int i, j;
            i = 23;
            j = i;
            Console.Write("Value Of i:{0}", i);
            Console.Write("\nValue Of j:{0}", j);
            i = 24;
            Console.Write("\nValue Of i:{0}", i);
            Console.Write("\nValue Of j:{0}", j);


            AgeClass ajit = new AgeClass();
            ajit.SetAge(23);

            AgeClass sumit = ajit; //Sumit now equals ajit

            Console.Write("\nAge Of AJIT:{0} ", ajit.GetAge());
            Console.WriteLine("\nAge Of Sumit:{0} ", sumit.GetAge());
            ajit.SetAge(24); //change Ajit's age, what Sumit to ajit now?
            Console.Write("\nAge Of AJIT:{0} ", ajit.GetAge());
            Console.WriteLine("\nAge Of Sumit:{0} ", sumit.GetAge());

            Console.WriteLine(ajit == sumit);
            Console.ReadLine();
        }
    }
}

public class AgeClass
{
    private int age;

    public void SetAge(int years)
    {
        age = years;
    }
    public int GetAge()
    {
        return age;
    }

    

}

 

As I know int is value type and sting or class is reference types. In above code value would be change in Age of Ajit 24 or Age of sumit 24. But if I am using string except class value won't change.

Here is modify code:-

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        
        static void Main(string[] args)
        {
            int i, j;
            i = 23;
            j = i;
            Console.Write("Value Of i:{0}", i);
            Console.Write("\nValue Of j:{0}", j);
            i = 24;
            Console.Write("\nValue Of i:{0}", i);
            Console.Write("\nValue Of j:{0}", j);


            //string x, y;
            //x = "Mohit";
            //y = x;
            //Console.WriteLine("X value is :"+ x);
            //Console.WriteLine("Y value is :" + y);

            //x = "Dahiya";
            //Console.WriteLine("X value is :" + x);
            //Console.WriteLine("Y value is :" + y);

            
            Console.ReadLine();
        }
    }
}

public class AgeClass
{
    private int age;

    public void SetAge(int years)
    {
        age = years;
    }
    public int GetAge()
    {
        return age;
    }

    

}

 

My question is - As string and class both are reference type.But in class case value of age will change and in string case Y value won't change.

Can anyone tell me why this is happening?

 

Thanks in advance.


Posted: 10/1/2010

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490
  Answered

In your first example object a and object b both points to same location so whatever is the changes it will affect both. In your second example 

string a='xxx'   a points to memory location say z

string b='fffff'   b points to memory location say y

a=b                 both a and b points to same memory loc now

a='kkkkk'         now a is pointing to diff location at this point b is still pointing to to old location so both of them will have        different values.


Posted: 10/1/2010

Professional 8338  points  Professional
  • Joined on: 4/15/2009
  • Posts: 424
  Answered

mohit said:

user="mohit kumar"]

My question is - As string and class both are reference type.But in class case value of age will change and in string case Y value won't change.

Can anyone tell me why this is happening?

Hi Mohit,

Strings are immutable reference types and so they don't behave quite like other reference types. I believe  have demonstrated you an example awhile back here:http://www.codeasp.net/forums/asp-net-topics/getting-started-general-asp-net/229/string


Posted: 10/1/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

raghav_khunger said:

user="Raghav Khunger"]

In your first example object a and object b both points to same location so whatever is the changes it will affect both. In your second example 

string a='xxx'   a points to memory location say z

string b='fffff'   b points to memory location say y

a=b                 both a and b points to same memory loc now

a='kkkkk'         now a is pointing to diff location at this point b is still pointing to to old location so both of them will have        different values.

 

Thank you for reply.

Its mean value types save in Stack and references types save in Heap.
But string case is different. String is references type and save in Heap. Like 

string x = "first"         Now "first" value(string value) save in stack and reference (address) save in Heap.

If I would change some value of x variable.Like

x = "second"     Now x reference(address) is different save.And later garbage collector remove the first reference(address).

 

Am I right Raghav Sir?


Posted: 10/1/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Vinz said:

user="Vincent Maverick Durano"]

 

mohit said:

user="mohit kumar"]

My question is - As string and class both are reference type.But in class case value of age will change and in string case Y value won't change.

Can anyone tell me why this is happening?

 

 

Hi Mohit,

Strings are immutable reference types and so they don't behave quite like other reference types. I believe  have demonstrated you an example awhile back here:http://www.codeasp.net/forums/asp-net-topics/getting-started-general-asp-net/229/string

 

Thanks Vinz.

Can you please tell me what is  immutable reference types ?


Posted: 10/1/2010

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490
  Answered

Mohit, 

First of all don't get confused . First look the context. I can prove you even for strings in your first example you will get both of them equal. I have not tested but with the below code you will still get both of them having same value:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {

        static void Main(string[] args)
        {
            int i, j;
            i = 23;
            j = i;
            Console.Write("Value Of i:{0}", i);
            Console.Write("\nValue Of j:{0}", j);
            i = 24;
            Console.Write("\nValue Of i:{0}", i);
            Console.Write("\nValue Of j:{0}", j);


            AgeClass ajit = new AgeClass();
            ajit.SetAge("23");

            AgeClass sumit = ajit; //Sumit now equals ajit

            Console.Write("\nAge Of AJIT:{0} ", ajit.GetAge());
            Console.WriteLine("\nAge Of Sumit:{0} ", sumit.GetAge());
            ajit.SetAge("24"); //change Ajit's age, what Sumit to ajit now?
            Console.Write("\nAge Of AJIT:{0} ", ajit.GetAge());
            Console.WriteLine("\nAge Of Sumit:{0} ", sumit.GetAge());

            Console.WriteLine(ajit == sumit);
            Console.ReadLine();
        }
    }
}

public class AgeClass
{
    private string age;

    public void SetAge(string years)
    {
        age = years;
    }
    public string GetAge()
    {
        return age;
    }



}



Posted: 10/1/2010

Professional 8338  points  Professional
  • Joined on: 4/15/2009
  • Posts: 424
  Answered

mohit said:

user="mohit kumar"]Can you please tell me what is  immutable reference types ?

Immutable means that after the object has been instantiated, a string cannot change value (see the example i've provided in the link I posted).On the other hand Mutable type is a type whose instance data can be modified. The System.Text.StringBuilder  class is an example of a mutable reference type. It contains members that can change the value of an instance of the class


Posted: 10/1/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

raghav_khunger said:

user="Raghav Khunger"]

Mohit, 

First of all don't get confused . First look the context. I can prove you even for strings in your first example you will get both of them equal. I have not tested but with the below code you will still get both of them having same value:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {

        static void Main(string[] args)
        {
            int i, j;
            i = 23;
            j = i;
            Console.Write("Value Of i:{0}", i);
            Console.Write("\nValue Of j:{0}", j);
            i = 24;
            Console.Write("\nValue Of i:{0}", i);
            Console.Write("\nValue Of j:{0}", j);


            AgeClass ajit = new AgeClass();
            ajit.SetAge("23");

            AgeClass sumit = ajit; //Sumit now equals ajit

            Console.Write("\nAge Of AJIT:{0} ", ajit.GetAge());
            Console.WriteLine("\nAge Of Sumit:{0} ", sumit.GetAge());
            ajit.SetAge("24"); //change Ajit's age, what Sumit to ajit now?
            Console.Write("\nAge Of AJIT:{0} ", ajit.GetAge());
            Console.WriteLine("\nAge Of Sumit:{0} ", sumit.GetAge());

            Console.WriteLine(ajit == sumit);
            Console.ReadLine();
        }
    }
}

public class AgeClass
{
    private string age;

    public void SetAge(string years)
    {
        age = years;
    }
    public string GetAge()
    {
        return age;
    }



}



 

 

Yes sir that I have already tested. Both of them having same value in object case. but my question was why doesn't the same value in string case.And as u said above u can prove in string have same value also.

HOW?Can you plz do?


Posted: 10/1/2010

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490

In the above example it is already proved since the referred object is same.


Posted: 10/1/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Vinz said:

user="Vincent Maverick Durano"]

 

mohit said:

user="mohit kumar"]Can you please tell me what is  immutable reference types ?

 

Immutable means that after the object has been instantiated, a string cannot change value (see the example i've provided in the link I posted).On the other hand Mutable type is a type whose instance data can be modified. The System.Text.StringBuilder  class is an example of a mutable reference type. It contains members that can change the value of an instance of the class

string string1 = "Hello";
string1.Replace ("Hello", "Hi");
string1 = string1.Replace ("Hello", "Hi");

 

I have run your code and understood about Immutable or mutable types.

Thanks VinjSmile


Posted: 10/1/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

raghav_khunger said:

user="Raghav Khunger"]

In the above example it is already proved since the referred object is same.

 

Thank you so much Sir.I have got the answerSmile 


Posted: 10/1/2010

Professional 8338  points  Professional
  • Joined on: 4/15/2009
  • Posts: 424

mohit said:

user="mohit kumar"]

string string1 = "Hello";
string1.Replace ("Hello", "Hi");
string1 = string1.Replace ("Hello", "Hi");

 

I have run your code and understood about Immutable or mutable types.

Thanks VinjSmile

Cool. Glad it clears out your confusion. :D


Page 1 of 1 (12 items)