Pages

Wednesday, 26 February 2014

Copy Constructor in C# with Example

Copy Constructor in C# with Example 


using System;
namespace ConsoleApplication3
{
    class Sample
    {
        public string param1, param2;
        public Sample(string x, string y)
        {
            param1 = x;
            param2 = y;
        }
        public Sample(Sample obj)     // Copy Constructor
        {
            param1 = obj.param1;
            param2 = obj.param2;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Sample obj = new Sample("Welcome", "http://fantasyaspnet.blogspot.in/");  // Create instance to class Sample
            Sample obj1 = new Sample(obj); // Here obj details will copied to obj1
            Console.WriteLine(obj1.param1 + " to " + obj1.param2);
            Console.ReadLine();
        }
    }
}



Welcome to http://fantasyaspnet.blogspot.in/


No comments:

Post a Comment