Showing posts with label Console Applications. Show all posts
Showing posts with label Console Applications. Show all posts

20 December 2013

String Vs StringBuilder

System.String
-------------
1. String is immutable. It means that you can't modify string at all, the result of modification is new string. This is not effective if you plan to append to string
2. many actions that you do with string
3. Here concatenation is used to combine two strings by using String object
4. The first string is combined to the other string by creating a new copy in the memory as a string object, and then the old string is deleted
5. string is non updatable
6. everytime a object has to be created for Operations like append,Insert etc. at runtime

System.StringBuilder
--------------------
1. StringBuilder is mutable. It can be modified in any way and it doesn't require creation of new instance.
2. if you try to do some other manipulation (like removing a part from the string, replacing a part in the string, etc.), then it's better not to use StringBuilder at those places. This is because we are anyway creating newstrings.
3. system.stringbuilder is updateble
4. string builder is faster than the string object
5.String builder is more efficient in case large amount of string operations have to be perform.
6.Insertion is done on the existing string.
7. At the end, we can get the string by StringBuilder.ToString()
dis-adv:
1. We must be careful to guess the size of StringBuilder. If the size which we are going to get is more than what is assigned, it must increase the size. This will reduce its performance.
2. many actions can't be done with StringBinder.
3. When initializing a StringBuilder, you are going down in performance.
4. StringBuilder can be used where more than four or more string concatenations take place.

19 September 2011

Operator OverLoading in console apps

class ClsComplex
{
int R, I;
public ClsComplex(int R, int I)
{
this.R = R;
this.I = I;
Console.WriteLine("{0} + {1}i", R, I);
}
public static ClsComplex operator +(ClsComplex Num1, ClsComplex Num2)
{
ClsComplex Num3 = new ClsComplex(Num1.R + Num2.R, Num1.I + Num2.I);
return Num3;
}
}
class ClsOpOverLoad
{
static void Main(string[] args)
{
Console.Write("C1= ");
ClsComplex C1 = new ClsComplex(4, 5);
Console.Write("C2= ");
ClsComplex C2 = new ClsComplex(8, 2);
Console.Write("C3= ");
ClsComplex C3 = C1 + C2;
Console.Read();
}
}

Method OverLoading in console apps

class mOverLoading
{
int x=10;
public void show()
{
Console.WriteLine("default constructor");
Console.WriteLine(x + this.x);
}
public void show(int x)
{
Console.WriteLine("Parameterized constructors");
Console.WriteLine(x+" "+this.x);
}
public void show(string s, int x)
{
Console.WriteLine("Parameterized constructor");
Console.WriteLine(s + " " + x);
}
public static void Main()
{
mOverLoading mol = new mOverLoading();
mol.show();
mol.show(123);
mol.show("satya", 456);
Console.ReadLine();
}
}

Constructor OverLoading in Console apps

class CACOverLoading
{
int x;
public CACOverLoading()
{
Console.WriteLine("Default constructor");
x = 123;
}
public CACOverLoading(int x)
{
Console.WriteLine("Parameterized constructor");
Console.WriteLine(x);
this.x = 789;
}
public void display()
{
Console.WriteLine(x);
}
public static void Main()
{
CACOverLoading col = new CACOverLoading();
col.display();
CACOverLoading col1 = new CACOverLoading(456);
col1.display();
Console.ReadLine();
}
}

Prime No. in console apps

class Prime
{
static void Main(string[] args)
{
int j = 0;
int count = 0;
Console.WriteLine("Plz enter a number: ");
int n =int.Parse( Console.ReadLine() );
Console.WriteLine("The Prime no's are:");
for (int i = 2; i <= n; i++)
{
for (j = 2; j <= i; j++)
{
if (i % j == 0)
{
break;
}
}
if (i == j)
{
Console.WriteLine(i);
count++;
}
}
Console.WriteLine();
Console.Write("Total Prime No's: "+count);
Console.ReadLine();
}

Virtual class overriding

class Virtual
{
public virtual void add(int a, int b)
{
Console.WriteLine(a + b);
}
}
class overridevirtual:Virtual
{
public override void add(int a, int b)
{
Console.WriteLine(a);
}
public static void Main()
{
Virtual vir = new Virtual();
vir.add(1,2);
Console.ReadLine();
}
}

Reverse of a string or number in console apps

class Reverseofno
{
public static void Main()
{
Console.WriteLine("enter a no/string: ");
string a = Console.ReadLine();
for (int x = a.Length - 1; x >= 0; x--)
{
Console.Write(a[x]);
}
Console.WriteLine();
--------------------------------------------------
string bb = string.Empty;
Console.WriteLine("enter a no/string: ");
string b = Console.ReadLine();
for (int x = b.Length - 1; x >= 0; x--)
{
bb += b[x].ToString();
}
string[] bbb = new string[bb.Split(' ').Length];
for (int y = 0; y = 0; z--)
{
Console.Write(bbb[z]);
}
Console.WriteLine();
---------------------------------------------------
Console.WriteLine("enter a name: ");
string n= Console.ReadLine();
if(bbb.Contains(n+" "))
Console.WriteLine("found");
else
Console.WriteLine("not found");
Console.ReadLine();
}
}