ShovelingLife
A Game Programmer
ShovelingLife
전체 방문자
오늘
어제
  • 분류 전체보기 (1074) N
    • 그래픽스 (57)
      • 공통 (19)
      • 수학 물리 (22)
      • OpenGL & Vulkan (1)
      • DirectX (14)
    • 게임엔진 (183)
      • Unreal (69)
      • Unity (103)
      • Cocos2D-X (3)
      • 개인 플젝 (8)
    • 코딩테스트 (221)
      • 공통 (7)
      • 프로그래머스 (22)
      • 백준 (162)
      • LeetCode (19)
      • HackerRank (2)
      • 코딩테스트 알고리즘 (8)
    • CS (235)
      • 공통 (21)
      • 네트워크 (44)
      • OS & 하드웨어 (55)
      • 자료구조 & 알고리즘 (98)
      • 디자인패턴 (6)
      • UML (4)
      • 데이터베이스 (7)
    • 프로그래밍 언어 (349) N
      • C++ (168) N
      • C# (90)
      • Java (9)
      • Python (33)
      • SQL (30)
      • JavaScript (8)
      • React (7)
    • 그 외 (10) N
      • Math (5)
      • 일상 (5)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • Source Code 좌측 상단에 복사 버튼 추가 완료
  • 언리얼 엔진 C++ 빌드시간 단축 꿀팁
  • 게임 업계 코딩테스트 관련
  • 1인칭 시점으로 써내려가는 글들

인기 글

태그

  • 배열
  • 백준
  • 프로그래머스
  • c#
  • 오블완
  • SQL
  • 그래픽스
  • 유니티
  • 문자열
  • C++
  • 함수
  • string
  • 언리얼
  • Unity
  • 파이썬
  • 알고리즘
  • 포인터
  • 티스토리챌린지
  • C
  • 클래스

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ShovelingLife

A Game Programmer

프로그래밍 언어/C#

C# 배열 복사

2023. 1. 8. 19:16

1. Array.CopyTo(Array destArray, int index) 

// srcArray의 데이터를 인자로 전달한 destArray에 저장한다.

// destArray의 마지막 인자의 인덱스 부터 저장된다. 

byte[] sourceArray = new byte[10]; // 복사를 할 배열
byte[] destinationArray = new byte[10]; // 복사를 당할 배열
 
// 복사 할 배열의 데이터를 삽입
for (int i = 0; i < 10; ++i)
    sourceArray[i] = (byte)i;
 
// 복사하기전 복사를 당할 배열의 데이터를 출력
for (int i = 0; i < 10; ++i)
    Console.Write($"{destinationArray[i]} ");
// 출력 결과 : 0 0 0 0 0 0 0 0 0 0
 
// 복사
// 복사를 당할 배열, 복사할 크기 (index의 갯수)
sourceArray.CopyTo(destinationArray, 0);
 
Console.WriteLine();
 
// 복사한 후 복사를 당할 배열의 데이터를 출력
for (int i = 0; i < 10; ++i)
    Console.Write($"{destinationArray[i]} ");
// 출력 결과 : 0 1 2 3 4 5 6 7 8 9
 
Console.WriteLine();

2. Array.Copy(Array srcArray, int srcCopyStartIndex, Array destArray, int destCopyStartIndex, int CopyLength)

// 예) srcArray[srcCopyStartIndex]에서 부터 CopyLength 까지 destArray에 복사.

// destArray의 destCopyStartIndex부터 저장.

byte[] sourceArray = new byte[10]; // 복사를 할 배열
byte[] destinationArray = new byte[10]; // 복사를 당할 배열
 
// 복사 할 배열의 데이터를 삽입
for (int i = 0; i < 10; ++i)
    sourceArray[i] = (byte)i;
 
// 복사하기전 복사를 당할 배열의 데이터를 출력
for (int i = 0; i < 10; ++i)
    Console.Write($"{destinationArray[i]} ");
// 출력 결과 : 0 0 0 0 0 0 0 0 0 0
 
/* 복사
   복사할 배열, 복사할 배열의 복사가 시작되는 위치,
   복사를 당할 배열, 복사를 당할 배열의 저장 시작 위치,
   크기 (index 갯수)
*/
Array.Copy(sourceArray, 0, destinationArray, 0, 10);
Console.WriteLine();
 
// 복사한 후 복사를 당할 배열의 데이터를 출력
for (int i = 0; i < 10; ++i)
    Console.Write($"{destinationArray[i]} " );
// 출력 결과 : 0 1 2 3 4 5 6 7 8 9
 
Console.WriteLine();

3. Array.Clone()

// 이 함수는 복사할 배열과 똑같은 복사본을 만들고 할당도 알아서 해준다.

byte[] sourceArray = new byte[10]; // 복사를 할 배열
 
// 복사 할 배열의 데이터를 삽입
for (int i = 0; i < 10; ++i)
    sourceArray[i] = (byte)i;
 
// sourceArray의 복사본을 똑같이 만들고. 할당도 알아서 해준다
byte[] destinationArray = sourceArray.Clone() as byte[];
 
// 복사한 후 복사를 당할 배열의 데이터를 출력
for (int i = 0; i < 10; ++i)
    Console.Write($"{destinationArray[i]} ");
// 출력 결과 : 0 1 2 3 4 5 6 7 8 9
 
Console.WriteLine();

4. Buffer.BlockCopy(Array srcArray, int srcOffset, Array destArray, int destOffset, int Length)

// Offset : 시작 위치다. 어디서부터 복사할지 결정 할 수 있다.

byte[] sourceArray = new byte[10]; // 복사를 할 배열
byte[] destinationArray = new byte[10]; // 복사를 당할 배열
 
// 복사 할 배열의 데이터를 삽입
for (int i = 0; i < 10; ++i)
    sourceArray[i] = (byte)i;
 
// 복사하기전 복사를 당할 배열의 데이터를 출력
for (int i = 0; i < 10; ++i)
    Console.Write($"{destinationArray[i]} ");
// 출력 결과 : 0 0 0 0 0 0 0 0 0 0
 
/* 복사
인자 (복사 할 배열, 복사할 배열의 복사 시작 위치, 복사를 당할 배열, 
복사를 당할 배열의 복사 시작 위치, 복사할 사이즈(인덱스의 갯수))*/
Buffer.BlockCopy(sourceArray, 0, destinationArray, 0, 10);
 
Console.WriteLine();
 
// 복사한 후 복사를 당할 배열의 데이터를 출력
for (int i = 0; i < 10; ++i)
    Console.Write($"{destinationArray[i]} ");
// 출력 결과 : 0 1 2 3 4 5 6 7 8 9
 
Console.WriteLine();

출처 : C# Programming - C#에서의 배열 복사 :: 게임 개발자의 블로그 (tistory.com)

저작자표시 (새창열림)

'프로그래밍 언어 > C#' 카테고리의 다른 글

C# Task 클래스  (0) 2023.03.24
C# foreach 사용법과 다양한 예제  (0) 2023.01.08
C# 배열 초기화, 다차원배열, 가변배열에 대해서  (0) 2023.01.07
C# 제네릭 형식 제약 조건 (where)  (0) 2023.01.06
C# string.format, 문자열 보간($)을 이용한 문자열 출력 방법  (0) 2023.01.05
    '프로그래밍 언어/C#' 카테고리의 다른 글
    • C# Task 클래스
    • C# foreach 사용법과 다양한 예제
    • C# 배열 초기화, 다차원배열, 가변배열에 대해서
    • C# 제네릭 형식 제약 조건 (where)
    ShovelingLife
    ShovelingLife
    Main skill stack => Unity C# / Unreal C++ Studying Front / BackEnd, Java Python

    티스토리툴바