Clone 메서드
이 문서는 기계 번역을 이용하여 번역되었습니다. 문서를 영문으로 보려면 영문 확인란을 선택하세요. 마우스 포인터를 텍스트 위로 이동시켜 팝업 창에서 영문 텍스트를 표시할 수도 있습니다.
번역
영문

Array.Clone 메서드 ()

 

Creates a shallow copy of the Array.

네임스페이스:   System
어셈블리:  mscorlib.dll의 mscorlib

public object Clone()

반환 값

Type: System.Object

A shallow copy of the Array.

A shallow copy of an T:System.Array copies only the elements of the T:System.Array, whether they are reference types or value types, but it does not copy the objects that the references refer to. The references in the new T:System.Array point to the same objects that the references in the original T:System.Array point to.

In contrast, a deep copy of an T:System.Array copies the elements and everything directly or indirectly referenced by the elements.

The clone is of the same T:System.Type as the original T:System.Array.

This method is an O(n) operation, where n is P:System.Array.Length.

The following code example clones a T:System.Globalization.CultureInfo array and demonstrates the behavior of a shallow copy.

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 ) );
   }

}


/* 
This code produces the following output.

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 -.

*/

Universal Windows Platform
4.5 이후에 사용 가능
.NET Framework
1.1 이후에 사용 가능
Portable Class Library
portable .NET platforms에서 지원 portable .NET platforms
Silverlight
2.0 이후에 사용 가능
Windows Phone Silverlight
7.0 이후에 사용 가능
Windows Phone
8.1 이후에 사용 가능
맨 위로 이동
표시:
© 2016 Microsoft