當前位置: 首頁>>代碼示例>>C#>>正文


C# Array.Clone方法代碼示例

本文整理匯總了C#中System.Array.Clone方法的典型用法代碼示例。如果您正苦於以下問題:C# Array.Clone方法的具體用法?C# Array.Clone怎麽用?C# Array.Clone使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Array的用法示例。


在下文中一共展示了Array.Clone方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

//引入命名空間
using System;
using System.Globalization;
public class SamplesArray  {

   public static void Main()  {

      // Create and initialize a new CultureInfo array.
      CultureInfo ci0 = new CultureInfo( "ar-SA", false );
      CultureInfo ci1 = new CultureInfo( "en-US", false );
      CultureInfo ci2 = new CultureInfo( "fr-FR", false );
      CultureInfo ci3 = new CultureInfo( "ja-JP", false );
      CultureInfo[] arrCI = new CultureInfo[] { ci0, ci1, ci2, ci3 };

      // Create a clone of the CultureInfo array.
      CultureInfo[] arrCIClone = (CultureInfo[]) arrCI.Clone();

      // Replace an element in the clone array.
      CultureInfo ci4 = new CultureInfo( "th-TH", false );
      arrCIClone[0] = ci4;

      // Display the contents of the original array.
      Console.WriteLine( "The original array contains the following values:" );
      PrintIndexAndValues( arrCI );

      // Display the contents of the clone array.
      Console.WriteLine( "The clone array contains the following values:" );
      PrintIndexAndValues( arrCIClone );

      // Display the DateTimeFormatInfo.DateSeparator for the fourth element in both arrays.
      Console.WriteLine( "Before changes to the clone:" );
      Console.WriteLine( "   Original: The DateTimeFormatInfo.DateSeparator for {0} is {1}.", arrCI[3].Name, arrCI[3].DateTimeFormat.DateSeparator );
      Console.WriteLine( "      Clone: The DateTimeFormatInfo.DateSeparator for {0} is {1}.", arrCIClone[3].Name, arrCIClone[3].DateTimeFormat.DateSeparator );

      // Replace the DateTimeFormatInfo.DateSeparator for the fourth element in the clone array.
      arrCIClone[3].DateTimeFormat.DateSeparator = "-";

      // Display the DateTimeFormatInfo.DateSeparator for the fourth element in both arrays.
      Console.WriteLine( "After changes to the clone:" );
      Console.WriteLine( "   Original: The DateTimeFormatInfo.DateSeparator for {0} is {1}.", arrCI[3].Name, arrCI[3].DateTimeFormat.DateSeparator );
      Console.WriteLine( "      Clone: The DateTimeFormatInfo.DateSeparator for {0} is {1}.", arrCIClone[3].Name, arrCIClone[3].DateTimeFormat.DateSeparator );
   }

   public static void PrintIndexAndValues( Array myArray )  {
      for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
         Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:48,代碼來源:Array.Clone

輸出:

The original array contains the following values:
        [0]:    ar-SA
        [1]:    en-US
        [2]:    fr-FR
        [3]:    ja-JP
The clone array contains the following values:
        [0]:    th-TH
        [1]:    en-US
        [2]:    fr-FR
        [3]:    ja-JP
Before changes to the clone:
   Original: The DateTimeFormatInfo.DateSeparator for ja-JP is /.
      Clone: The DateTimeFormatInfo.DateSeparator for ja-JP is /.
After changes to the clone:
   Original: The DateTimeFormatInfo.DateSeparator for ja-JP is -.
      Clone: The DateTimeFormatInfo.DateSeparator for ja-JP is -.

示例2: Main

//引入命名空間
using System;
using System.Collections.Generic;

public class Starter {
    public static void Main() {
        CommissionedEmployee[] salespeople =
                {new CommissionedEmployee("Bob"),
                 new CommissionedEmployee("Ted"),
                 new CommissionedEmployee("Sally")};

        Employee[] employees =
            (Employee[])salespeople.Clone();

        foreach (Employee person in
                employees) {
            person.Pay();
        }
    }
}

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 CommissionedEmployee : Employee {
    public CommissionedEmployee(string name) :
        base(name) {
    }

    public override void Pay() {
        base.Pay();
        Console.WriteLine("Paying commissions");
    }

}
開發者ID:C#程序員,項目名稱:System,代碼行數:44,代碼來源:Array.Clone


注:本文中的System.Array.Clone方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。