C#/C# Tutorial

C# Arrays and Array.Copy | C# Tutorial for Beginners

DragonTory 2023. 2. 14. 21:53
반응형

 C# Arrays 

You can store multiple variables of the same type in an array data structure.

You declare an array by specifying the type of its elements. If you want the array to store elements of any type, you can specify object as its type.

Each element in the array is identified by an index, which is an integer that starts at 0 for the first element and increases by 1 for each subsequent element.

You can declare an array in C# like this:

type[] arrayName = new type[length];

where type is the data type of the elements in the array, arrayName is the name of the array, and length is the number of elements in the array. 

For example:

int[] numbers = new int[5];

 

This creates an array of integers with 5 elements.

You can also initialize the elements of an array when you declare it, like this:

int[] numbers = { 1, 2, 3, 4, 5 };

This creates an array of integers with 5 elements and initializes each element with the specified values.

 

You can access the elements of an array using the index, like this:

int[] numbers = { 1, 2, 3, 4, 5 };
int secondNumber = numbers[1]; // secondNumber is 2

 

You can also modify the elements of an array using the index:

int[] numbers = { 1, 2, 3, 4, 5 };
numbers[1] = 10; // changes the second element to 10

 

Arrays have a Length property that you can use to get the number of elements in the array:

int[] numbers = { 1, 2, 3, 4, 5 };
int length = numbers.Length; // length is 5

 

You can iterate over the elements of an array using a for loop:

int[] numbers = { 1, 2, 3, 4, 5 };
for (int i = 0; i < numbers.Length; i++)
{
    Console.WriteLine(numbers[i]);
}

 

This will output each element of the array to the console.

 

Examples:

// Declare an array of integers with 5 elements
int[] numbers = new int[5];

// Declare and initialize an array of integers with 5 elements
int[] numbers = { 1, 2, 3, 4, 5 };

// Declare and initialize a two-dimensional array of integers with 3 rows and 2 columns
int[,] matrix = { { 1, 2 }, { 3, 4 }, { 5, 6 } };

// Declare an array of strings with 3 elements
string[] names = new string[3];

// Declare and initialize an array of strings with 3 elements
string[] names = { "Alice", "Bob", "Charlie" };



Note that arrays in C# are fixed-size, which means that you cannot add or remove elements from an array once it has been created.

If you need a dynamic data structure that can grow or shrink as needed, you can use a List instead.

 

 C# Array.Copy 

In C#, you can copy one array to another using the Array.Copy method. 

This method takes three arguments: the source array, the destination array, and the number of elements to copy. 

Here's an example:

int[] source = { 1, 2, 3 };
int[] destination = new int[3];

Array.Copy(source, destination, 3);

// destination now contains { 1, 2, 3 }

 

In this example, we have an array called source with three elements. 

We also have an empty array called destination with three elements. 

We then use the Array.Copy method to copy the elements from source to destination. 

The third argument to Array.Copy is the number of elements to copy, which in this case is the length of the array.

After the Array.Copy method is called, the destination array will contain the same elements as the source array. 

Note that the Array.Copy method performs a shallow copy, which means that if the elements in the array are reference types, the references are copied rather than the objects themselves.

반응형