How to Work With Array.Clone Method in C#

In this article I am going to explain that how to create Clone Method in Array.
  • 3934

C# provide a inbuilt function Array.Clone() which are used to Create a shallow copy of the Array. Clone will copy the structure of a data A shallow copy of an Array copies only the elements of the Array but it does not copy the objects. If array is a reference type array, then it does not copy the objects referenced by the elements.

using System.Collections.Generic;

 

namespace demo_array

{

    public class class1 {

    public static void Main() {

        CEmp[] salespeople =

                {new CEmp("Bob"),

                 new CEmp("Ted"),

                 new CEmp("Sally")};

 

        Employee[] employees =

            (Employee[])salespeople.Clone();

 

        foreach (Employee person in

                employees) {

            person.Pay();

            Console.ReadKey();

        }

    }

}

 

public class Employee {

    public Employee(string name) {

        m_Name = name;

    }

 

    public virtual void Pay() {

        Console.WriteLine("Paying {0}", m_Name);

    }

 

    private string m_Name;

}

 

public class CEmp: Employee

{

    public CEmp(string name) :

        base(name)

    {

    }

 

    public override void Pay()

    {

        base.Pay();

        Console.WriteLine("Paying commissions");

    }

}

}

Output

clone method.gif

Ask Your Question 

Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.