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


C# Array.CopyTo方法代码示例

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


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

示例1: Main

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

   public static void Main()  {

      // Creates and initializes two new Arrays.
      Array mySourceArray=Array.CreateInstance( typeof(String), 6 );
      mySourceArray.SetValue( "three", 0 );
      mySourceArray.SetValue( "napping", 1 );
      mySourceArray.SetValue( "cats", 2 );
      mySourceArray.SetValue( "in", 3 );
      mySourceArray.SetValue( "the", 4 );
      mySourceArray.SetValue( "barn", 5 );
      Array myTargetArray=Array.CreateInstance( typeof(String), 15 );
      myTargetArray.SetValue( "The", 0 );
      myTargetArray.SetValue( "quick", 1 );
      myTargetArray.SetValue( "brown", 2 );
      myTargetArray.SetValue( "fox", 3 );
      myTargetArray.SetValue( "jumps", 4 );
      myTargetArray.SetValue( "over", 5 );
      myTargetArray.SetValue( "the", 6 );
      myTargetArray.SetValue( "lazy", 7 );
      myTargetArray.SetValue( "dog", 8 );

      // Displays the values of the Array.
      Console.WriteLine( "The target Array contains the following (before and after copying):" );
      PrintValues( myTargetArray, ' ' );

      // Copies the source Array to the target Array, starting at index 6.
      mySourceArray.CopyTo( myTargetArray, 6 );

      // Displays the values of the Array.
      PrintValues( myTargetArray, ' ' );
   }

   public static void PrintValues( Array myArr, char mySeparator )  {
      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( "{0}{1}", mySeparator, myEnumerator.Current );
      }
      Console.WriteLine();
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:52,代码来源:Array.CopyTo

输出:

The target Array contains the following (before and after copying):
 The quick brown fox jumps over the lazy dog      
 The quick brown fox jumps over three napping cats in the barn

示例2: Main

public class SamplesArray2{

   public static void Main()  {
      // Creates and initializes the source Array.
      Array myArrayZero=Array.CreateInstance( typeof(String), 3 );
      myArrayZero.SetValue( "zero", 0 );
      myArrayZero.SetValue( "one", 1 );

      // Displays the source Array.
      Console.WriteLine( "The array with lower bound=0 contains:" );
      PrintIndexAndValues( myArrayZero );

      // Creates and initializes the target Array.
      int[] myArrLen = { 4 };
      int[] myArrLow = { 2 };
      Array myArrayTwo=Array.CreateInstance( typeof(String), myArrLen, myArrLow );
      myArrayTwo.SetValue( "two", 2 );
      myArrayTwo.SetValue( "three", 3 );
      myArrayTwo.SetValue( "four", 4 );
      myArrayTwo.SetValue( "five", 5 );

      // Displays the target Array.
      Console.WriteLine( "The array with lower bound=2 contains:" );
      PrintIndexAndValues( myArrayTwo );

      // Copies from the array with lower bound=0 to the array with lower bound=2.
      myArrayZero.CopyTo( myArrayTwo, 3 );

      // Displays the modified target Array.
      Console.WriteLine( "\nAfter copying to the target array from index 3:" );
      PrintIndexAndValues( myArrayTwo );
   }

   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,代码行数:38,代码来源:Array.CopyTo

输出:

The array with lower bound=0 contains:
    [0]:    zero
    [1]:    one
    [2]:    
The array with lower bound=2 contains:
    [2]:    two
    [3]:    three
    [4]:    four
    [5]:    five

After copying to the target array from index 3:
    [2]:    two
    [3]:    zero
    [4]:    one
    [5]:

示例3: Main

//引入命名空间
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;


public class MainClass
{

    public static void Main()
    {
        int[] srcArray = new int[] { 1, 2, 3 };
        int[] destArray = new int[] { 4, 5, 6, 7, 8, 9 };

        Array.Copy(srcArray, destArray, srcArray.Length);
        srcArray.CopyTo(destArray, 0);
        srcArray.CopyTo(destArray, 3);
        Array.Copy(srcArray, 0, destArray, 2, srcArray.Length);

    }
}
开发者ID:C#程序员,项目名称:System,代码行数:23,代码来源:Array.CopyTo


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