By Sadiq Ahmed
Purpose
The main purpose of this article is to explore some common and uncommon issues regarding DotNet and C#. Among such common things my first article is on string data type which is reference type but some developers don’t understand it’s behavior when compare with others reference type.
Problem
Changing the value of an alias reflect the change in the actual class object and vice versa.
Explanation
Reference Type
Let assume that we have a class called MyType. There is another class called AppType. MyType has a property called Name. AppType is simply used to run the application by providing Main method.
Here, is the code
//Program#1
using System;
using System.Console;
class MyType
{
private string name;
private string Name
{ set
{
name=value;
}
get
{
return name;
}
}
}
class AppType
{
public static void Main()
{
MyType obj1,obj2;
Console.WriteLine("*****Learning reference philosophy*****");
obj2=new MyType();
obj2.Name="Sadiq";
obj1=obj2;
Console.WriteLine("values of obj1={0} and obj2={1}",obj1.Name,obj2.Name);
obj1.Name="Ahmed";
Console.WriteLine("values of obj1={0} and obj2={1}",obj1.Name,obj2.Name);
}
}
when you compile the code, you’ll get the following output
*****Learning reference philosophy*****
values of obj1=Sadiq and obj2=Sadiq
values of obj1=Ahmed and obj2=Ahmed
It means that object obj1 is nothing except the alias of obj2.In other words, both obj1 and obj2 are pointing to the same memory.
Value Type
Let assume that we have a structure called MyType. There is another structure called AppType. MyType has a property called Name. AppType is simply used to run the application by providing Main method.
Here, is the code
//Program#2
using System;
using System.Console;
class MyType
{
private string name;
private string Name
{ set
{
name=value;
}
get
{
return name;
}
}
}
class AppType
{
public static void Main()
{
MyType obj1,obj2;
Console.WriteLine("*****Learning reference philosophy*****");
obj2=new MyType();
obj2.Name="Sadiq";
obj1=obj2;
Console.WriteLine("values of obj1={0} and obj2={1}",obj1.Name,obj2.Name);
obj1.Name="Ahmed";
Console.WriteLine("values of obj1={0} and obj2={1}",obj1.Name,obj2.Name);
}
}
when you compile the code, you’ll get the following output
*****Learning reference philosophy*****
values of obj1=Sadiq and obj2=Sadiq
values of obj1=Ahmed and obj2=Sadiq
It means that object obj1 and obj2 both are different .In other words, both obj1 and obj2 are distinct and pointing to the different memory.
Reference or Value type
Now, consider String type instead of user defined data type. The above code will become,
//Program#3
class AppType
{
public static void Main()
{
String obj1,obj2;
Console.WriteLine("*****Learning reference philosophy*****");
//No need of it
//obj2=new MyType();
obj2="Sadiq";
obj1=obj2;
Console. WriteLine("values of obj1={0} and obj2={1}",obj1,obj2);
obj1="Ahmed";
Console.WriteLine("values of obj1={0} and obj2={1}",obj1,obj2);
}
}
when you compile the code, you’ll get the following output
*****Learning reference philosophy*****
values of obj1=Sadiq and obj2=Sadiq
values of obj1=Ahmed and obj2=Sadiq
It means that object obj1 is not an alias of obj2.In other words, both obj1 and obj2 are pointing to the different memory.
Strange!
Now, we all know that String type object dynamically grow. It implies that it must allocate its memory on the Heap. Since, reference type allocates the memory on Heap, therefore, it should be reference type.
If it is reference type than why it behaves as value type.
Reason
The fact lies in these following two lines,
string obj1;
obj1 = “value forces to allocate a memory”;
The first line is nothing except the declaration of an object while second line actually creates the object. It means that you can conceptually relate second line to
obj=new string();.
Summary
Therefore, whenever the value of a string object initialized or assigned it creates an
object in memory.
Now, we It means that object obj1 is not an alias of obj2.In other words, both obj1 and obj2 are pointing to the different memory.