Wednesday, October 6, 2010

Reference Type Example

Notes on Refernce Type.
e.g Class, String, Array,Exception etc.
1. Conventionally, the term object refers to an instance of a reference type, whereas the term value refers to
an instance of a value type, but all instances of any type (reference type or value type) are also derived from type
object.

2. Reference of a object stores in Stack but Actual object stores on Heap
//Both the below references 'a' & 'b' will be stored on Stack
CLassA a;
ClassA b;

//Both the below object 'aa' & 'bb' will be stored on Heap.

ClassA aa=new ClassA();
ClassA bb=new ClassA();


IMMUTABLE vs MUTTABLE:
string name = "Bimlesh";
name += "Kumar";
name += "singh";
//In this scenerio 3 copy of name will be created and last one will be latest one.
//name="Bimlesh";
//name="Bimlesh Kumar";
//name="Bimlesh Kumar Singh";

//To avoid this use either StringBuiler or join/Concat/FormatString
Console.WriteLine(name);

StringBuilder sb = new StringBuilder("Bimlesh");
sb.Append("Singh");
Console.WriteLine(sb.ToString());
Console.Read();

No comments: