当前位置: 首页>>代码示例>>C#>>正文


C# Array.Copy方法代码示例

本文整理汇总了C#中System.Array.Copy方法的典型用法代码示例。如果您正苦于以下问题:C# Array.Copy方法的具体用法?C# Array.Copy怎么用?C# Array.Copy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Array的用法示例。


在下文中一共展示了Array.Copy方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a new Array of type Int32.
      Array myIntArray=Array.CreateInstance( typeof(System.Int32), 5 );
      for ( int i = myIntArray.GetLowerBound(0); i <= myIntArray.GetUpperBound(0); i++ )
         myIntArray.SetValue( i+1, i );

      // Creates and initializes a new Array of type Object.
      Array myObjArray = Array.CreateInstance( typeof(System.Object), 5 );
      for ( int i = myObjArray.GetLowerBound(0); i <= myObjArray.GetUpperBound(0); i++ )
         myObjArray.SetValue( i+26, i );

      // Displays the initial values of both arrays.
      Console.WriteLine( "Int32 array:" );
      PrintValues( myIntArray );
      Console.WriteLine( "Object array:" );
      PrintValues( myObjArray );

      // Copies the first element from the Int32 array to the Object array.
      Array.Copy( myIntArray, myIntArray.GetLowerBound(0), myObjArray, myObjArray.GetLowerBound(0), 1 );

      // Copies the last two elements from the Object array to the Int32 array.
      Array.Copy( myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2 );

      // Displays the values of the modified arrays.
      Console.WriteLine( "Int32 array - Last two elements should now be the same as Object array:" );
      PrintValues( myIntArray );
      Console.WriteLine( "Object array - First element should now be the same as Int32 array:" );
      PrintValues( myObjArray );
   }

   public static void PrintValues( Array myArr )  {
      System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
      int i = 0;
      int cols = myArr.GetLength( myArr.Rank - 1 );
      while ( myEnumerator.MoveNext() )  {
         if ( i < cols )  {
            i++;
         } else  {
            Console.WriteLine();
            i = 1;
         }
         Console.Write( "\t{0}", myEnumerator.Current );
      }
      Console.WriteLine();
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:51,代码来源:Array.Copy

输出:

Int32 array:
    1    2    3    4    5
Object array:
    26    27    28    29    30
Int32 array - Last two elements should now be the same as Object array:
    1    2    3    29    30
Object array - First element should now be the same as Int32 array:
    1    27    28    29    30

示例2: Main

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Copy an array. 
 
using System;  
  
public class CopyDemo {     
  public static void Main() {     
    int[] source = { 1, 2, 3, 4, 5 }; 
    int[] target = { 11, 12, 13, 14, 15 }; 
    int[] source2 = { -1, -2, -3, -4, -5 }; 
 
    // Display source. 
    Console.Write("source: "); 
    foreach(int i in source)  
      Console.Write(i + " "); 
    Console.WriteLine(); 
 
    // Display original target. 
    Console.Write("Original contents of target: "); 
    foreach(int i in target)  
      Console.Write(i + " "); 
    Console.WriteLine(); 
 
    // Copy the entire array. 
    Array.Copy(source, target, source.Length); 
 
    // Display copy. 
    Console.Write("target after copy:  "); 
    foreach(int i in target)  
      Console.Write(i + " "); 
    Console.WriteLine(); 
 
    // Copy into middle of target. 
    Array.Copy(source2, 2, target, 3, 2); 
 
    // Display copy. 
    Console.Write("target after copy:  "); 
    foreach(int i in target)  
      Console.Write(i + " "); 
    Console.WriteLine(); 
  } 
}
开发者ID:C#程序员,项目名称:System,代码行数:49,代码来源:Array.Copy


注:本文中的System.Array.Copy方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。