logo

C# 배열

다른 프로그래밍 언어와 마찬가지로 C#의 배열은 연속적인 메모리 위치를 갖는 유사한 유형의 요소 그룹입니다. C#에서 배열은 물체 기본 유형 시스템.배열 . C#에서 배열 인덱스는 0부터 시작합니다. C# 배열에는 고정된 요소 집합만 저장할 수 있습니다.

C# 배열

C# 배열의 장점

  • 코드 최적화(코드 감소)
  • 랜덤 액세스
  • 데이터를 쉽게 탐색할 수 있음
  • 데이터 조작이 쉬움
  • 데이터 등의 정렬이 용이합니다.

C# 배열의 단점

  • 고정 크기

C# 배열 유형

C# 프로그래밍에는 세 가지 유형의 배열이 있습니다.

  1. 단일차원 배열
  2. 다차원 배열
  3. 들쭉날쭉한 배열

C# 1차원 배열

1차원 배열을 생성하려면 유형 뒤에 대괄호 []를 사용해야 합니다.

 int[] arr = new int[5];//creating array 

식별자 뒤에 대괄호를 넣을 수 없습니다.

C의 부울
 int arr[] = new int[5];//compile time error 

배열을 선언하고 초기화하고 탐색할 C# 배열의 간단한 예를 살펴보겠습니다.

 using System; public class ArrayExample { public static void Main(string[] args) { int[] arr = new int[5];//creating array arr[0] = 10;//initializing array arr[2] = 20; arr[4] = 30; //traversing array for (int i = 0; i <arr.length; i++) { console.writeline(arr[i]); } < pre> <p>Output:</p> <pre> 10 0 20 0 30 </pre> <h3>C# Array Example: Declaration and Initialization at same time</h3> <p>There are 3 ways to initialize array at the time of declaration.</p> <pre> int[] arr = new int[5]{ 10, 20, 30, 40, 50 }; </pre> <p>We can omit the size of array.</p> <pre> int[] arr = new int[]{ 10, 20, 30, 40, 50 }; </pre> <p>We can omit the new operator also.</p> <pre> int[] arr = { 10, 20, 30, 40, 50 }; </pre> <p>Let&apos;s see the example of array where we are declaring and initializing array at the same time.</p> <pre> using System; public class ArrayExample { public static void Main(string[] args) { int[] arr = { 10, 20, 30, 40, 50 };//Declaration and Initialization of array //traversing array for (int i = 0; i <arr.length; i++) { console.writeline(arr[i]); } < pre> <p>Output:</p> <pre> 10 20 30 40 50 </pre> <h3>C# Array Example: Traversal using foreach loop</h3> <p>We can also traverse the array elements using foreach loop. It returns array element one by one.</p> <pre> using System; public class ArrayExample { public static void Main(string[] args) { int[] arr = { 10, 20, 30, 40, 50 };//creating and initializing array //traversing array foreach (int i in arr) { Console.WriteLine(i); } } } </pre> <p>Output:</p> <pre> 10 20 30 40 50 </pre></arr.length;></pre></arr.length;>

C# 배열 예제: 선언과 초기화를 동시에 수행

선언시 배열을 초기화하는 방법에는 3가지가 있습니다.

 int[] arr = new int[5]{ 10, 20, 30, 40, 50 }; 

배열의 크기는 생략할 수 있습니다.

 int[] arr = new int[]{ 10, 20, 30, 40, 50 }; 

new 연산자도 생략할 수 있습니다.

 int[] arr = { 10, 20, 30, 40, 50 }; 

배열을 선언하고 동시에 초기화하는 배열의 예를 살펴보겠습니다.

 using System; public class ArrayExample { public static void Main(string[] args) { int[] arr = { 10, 20, 30, 40, 50 };//Declaration and Initialization of array //traversing array for (int i = 0; i <arr.length; i++) { console.writeline(arr[i]); } < pre> <p>Output:</p> <pre> 10 20 30 40 50 </pre> <h3>C# Array Example: Traversal using foreach loop</h3> <p>We can also traverse the array elements using foreach loop. It returns array element one by one.</p> <pre> using System; public class ArrayExample { public static void Main(string[] args) { int[] arr = { 10, 20, 30, 40, 50 };//creating and initializing array //traversing array foreach (int i in arr) { Console.WriteLine(i); } } } </pre> <p>Output:</p> <pre> 10 20 30 40 50 </pre></arr.length;>

C# 배열 예제: foreach 루프를 사용한 순회

foreach 루프를 사용하여 배열 요소를 탐색할 수도 있습니다. 배열 요소를 하나씩 반환합니다.

 using System; public class ArrayExample { public static void Main(string[] args) { int[] arr = { 10, 20, 30, 40, 50 };//creating and initializing array //traversing array foreach (int i in arr) { Console.WriteLine(i); } } } 

산출:

 10 20 30 40 50