
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Array Copy in C#
Use the array. copy method in C# to copy a section of one array to another.
Our original array has 10 elements −
int [] n = new int[10]; /* n is an array of 10 integers */
Our new array that would copy a section of array 1 has 5 elements −
int [] m = new int[5]; /* m is an array of 5 integers */
The array.copy() method allow you to add the source and destination array. With that, include the size of the section of the first array that includes in the second array.
Example
You can try to run the following to implement Array copy in C# −
using System; namespace ArrayApplication { class MyArray { static void Main(string[] args) { int [] n = new int[10]; /* n is an array of 10 integers */ int [] m = new int[5]; /* m is an array of 5 integers */ for ( int i = 0; i < 10; i++ ) { n[i] = i + 100; } Console.WriteLine("Original Array..."); foreach (int j in n ) { int i = j-100; Console.WriteLine("Element[{0}] = {1}", i, j); } Array.Copy(n, 0, m, 0, 5); Console.WriteLine("New Array..."); foreach (int res in m) { Console.WriteLine(res); } Console.ReadKey(); } } }
Output
Original Array... Element[0] = 100 Element[1] = 101 Element[2] = 102 Element[3] = 103 Element[4] = 104 Element[5] = 105 Element[6] = 106 Element[7] = 107 Element[8] = 108 Element[9] = 109 New Array... 100 101 102 103 104
- Related Articles
- Array Copy in Java
- Copy char array to string in Java
- Copy the entire LinkedList to Array in C#
- Copy the Stack to an Array in C#
- Copy characters from string into char Array in Java
- Copy values from one array to another in Numpy
- Return a copy of the masked array in NumPy
- How to copy a section of an array into another array in C#?
- Java Program to copy an array from the specified source array
- Copy all elements in Java TreeSet to an Object Array
- How can we copy one array from another in Java
- Copy StringCollection at the specified index of array in C#
- Copy StringDictionary to Array at the specified index in C#
- How to copy collection to Array using C#?
- Python program to copy all elements of one array into another array

Advertisements