Monday, February 8, 2010

C# Framework Fundamentals

1. Common Reference Type
i) String and String Builder
Strings are immutable, which mean in the run time it will create the new string and abundant the old one,
ex :-
String s = new String();
s+="string1";
s+="string2";

string Builder class is mutable it will create at dynamicaly and we can defined its length what ever we want.
ex :- System.Text.StringBuilder sb = new System.Text.StringBuilder(30);
sb.Append("string1");
sb.Append("string2 ");
string s = sb.Tostring();


Generics
reduced runtime error,
improve runtime performance

example : OBject class without Genarics :-

Class Object (){
public Object t;
public object u;
public object(object t1, object u1){
t = t1;
u = u1; }
}
add two string to object class
Object obj = new Object("string1","string2");
console.writeline((string)obj.t + (string)obj.U);

the above way can be used using genarics
class gen
{
public T t;
public U u;
public gen(T t1, U u1){
t =t1;
u = U1;}
}

add two string to Gen class
Gen gen = new Gen("string1","string2");
console.writeline(gen.t + gen.U);
//above methode no need to object cast , also it does not occur runtime error, (int double cast error);

No comments:

Post a Comment