C# String Type Object Peculiarities

Purpose

The main purpose of this article is to exploresome common and uncommon issues regarding DotNet and C#. Among suchcommon things my first article is on string data type which isreference type but some developers don?t understand it?s behavior whencompare 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 calledName. AppType is simply used to run the application by providing Mainmethod.

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 exceptthe alias of obj2.In other words, both obj1 and obj2 are pointing tothe same memory.

Value Type

Let assume that we have a structure calledMyType. There is another structure called AppType. MyType has aproperty called Name. AppType is simply used to run the application byproviding 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 aredifferent .In other words, both obj1 and obj2 are distinct and pointingto 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 objectdynamically grow. It implies that it must allocate its memory on theHeap. Since, reference type allocates the memory on Heap, therefore, itshould 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 thedeclaration 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 analias of obj2.In other words, both obj1 and obj2 are pointing to thedifferent memory.

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!