Overloaded Constructor

By overloading the constructor, you are giving flexibility to users the way in which objects can be created. Invoking an overloaded constructor using this, to invoke an overloaded constructor, you can use this keyword.

E.g.:    using System;

class Demo

{

int x;

int y;

Demo(int i, int j)

{

Console.WriteLine("invoking two arg constructor");

x = i;

y = j;

}

Demo(int x) : this(x,x)

{

Console.WriteLine("invoking the one arg constructor");

}

Demo() : this(0,0)

{

Console.WriteLine("invoking the default constructor");

}

Demo( Demo d) : this(d.x, d.y)

{

{

Console.WriteLine("invoking the one arg constructor - object");

}

}

}

0 comments :

Post a Comment

> Related Posts with Thumbnails
 

Copyright © 2012. GS dot net - All Rights Reserved - Design by BTDesigner - Proudly powered by Blogger