Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

Array.ConstrainedCopy() วิธีการใน C #


วิธีการ Array.ConstrainedCopy() ใน C# ใช้เพื่อคัดลอกช่วงขององค์ประกอบจาก Array โดยเริ่มจากดัชนีต้นทางที่ระบุ และวางลงใน Array อื่นโดยเริ่มต้นที่ดัชนีปลายทางที่ระบุ

ไวยากรณ์

public static void ConstrainedCopy (Array sourceArr, int sourceIndex, Array destinationArr, int destinationIndex, int length);

ที่นี่

  • sourceArr - อาร์เรย์ที่มีข้อมูลที่จะคัดลอก

  • sourceIndex - จำนวนเต็ม 32 บิตที่แสดงถึงดัชนีใน sourceArr ที่การคัดลอกเริ่มต้น

  • destinationArr − Array ที่รับข้อมูล

  • destinationIndex - จำนวนเต็ม 32 บิตที่แสดงถึงดัชนีใน destinationArr ที่การจัดเก็บเริ่มต้น

  • len − จำนวนเต็ม 32 บิตที่แสดงจำนวนองค์ประกอบที่จะคัดลอก

ให้เราดูตัวอย่างการใช้เมธอด Array.ConstrainedCopy() -

ตัวอย่าง

using System;
public class Demo{
   public static void Main(){
      int[] arrDest = new int[10];
      Console.WriteLine("Array elements...");
      int[] arrSrc = { 20, 50, 100, 150, 200, 300, 400};
      for (int i = 0; i < arrSrc.Length; i++){
         Console.Write("{0} ", arrSrc[i]);
      }
      Console.WriteLine();
      Array.ConstrainedCopy(arrSrc, 3, arrDest, 0, 4);
      Console.WriteLine("Destination Array: ");
      for (int i = 0; i < arrDest.Length; i++){
         Console.Write("{0} ", arrDest[i]);
      }
   }
}

ผลลัพธ์

สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

Array elements...
20 50 100 150 200 300 400
Destination Array:
150 200 300 400 0 0 0 0 0 0