By 22/07/2015 7 Comments

C# Pass Object To Constructor As Parameter

  • In C# using inheritance an object of a derived class can be passed to its base class constructor as a parameter, it is possible because a base class reference can refers to a derived class object.
  • Base class constructor initializes/constructs only derived class members which has also its own members.
  • One object is created from another, or a copy of one object can be made from other.

Example:

using System;
namespace csharpBasic
{
    class Box
    {
        // Fields declaration with protected access.
        protected int Width;
        protected int Height;

        // Constructor declaration which pass an object reference as a parameter.
        public Box(Box box)
        {
            this.Width = box.Width;
            this.Height = box.Height;
        }

        // Constructor with 2 parameters.
        public Box(int width, int height)
        {
            this.Width = width;
            this.Height = height;
        }
    }

    class GreenBox : Box
    {
        string Color;

        // Derived class constructor pass 3 parameters.
        public GreenBox(int width, int height, string color)
            : base(width, height)
        {
            this.Color = color;
        }

        // Derived constructor declaration which pass an object reference as a parameter.
        public GreenBox(GreenBox greenBox)
            : base(greenBox)
        {
            this.Color = greenBox.Color;
        }

        // Different return type methods.
        public string GetColor()
        {
            return this.Color;
        }

        public int GetWidth()
        {
            return this.Width;
        }

        public int GetHeight()
        {
            return this.Height;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Object creation of derived greenBox1 of GreenBox type.
            GreenBox greenBox1 = new GreenBox(50, 30, "Green");

            // Another object greenBox2 is being created from greenBox1.
            GreenBox greenBox2 = new GreenBox(greenBox1);

            // Following statements print the different information about objects.
            Console.WriteLine("Green box 1:");
            Console.WriteLine("Color: {0}", greenBox1.GetColor());
            Console.WriteLine("Width: {0}", greenBox1.GetWidth());
            Console.WriteLine("Height: {0}", greenBox1.GetHeight());
            Console.WriteLine();
            Console.WriteLine("Green box 2:");
            Console.WriteLine("Color: {0}", greenBox2.GetColor());
            Console.WriteLine("Width: {0}", greenBox2.GetWidth());
            Console.WriteLine("Height: {0}", greenBox2.GetHeight());
            Console.ReadKey();
        }
        /*
        The Output will be:
        
        Green box 1:
        Color: Green
        Width: 50
        Height: 30

        Green box 2:
        Color: Green
        Width: 50
        Height: 30
         
       */
    }
}






Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .