Posts C# - 배열 내부를 같은 값으로 채우기
Post
Cancel

C# - 배열 내부를 같은 값으로 채우기

Memo


1
2
// 인덱스 0부터 99까지 정수 1로 채우기
int[] arr = Enumerable.Repeat(1, 100).ToArray();

물론 LINQ를 쓰는 만큼, 중간 버퍼의 가비지는 감안해야 한다.


.NET 5.0 버전이라면 Array.Fill() 메소드를 사용하면 된다.

1
2
int[] arr = new int[100];
Array.Fill(arr, 1); // 배열 전체에 1로 채우기
This post is licensed under CC BY 4.0 by the author.