C#
[C#] foreach
키베이루
2022. 12. 21. 00:08
foreach 문은 인자로 들어온 데이터의 내부 인덱스 끝까지 순환하는 반복문이다.
foreach(데이터 형식 변수명 in 배열(문자열)) {}로 사용한다.
int [] arr = new int[7] = {1, 2, 3, 4, 5, 6, 7};
foreach(int i in arr){
Console.WriteLine("{0}",i) // 1 2 3 4 5 6 7 출력
}
string stringText = "Hello World!";
foreach(char oneCharactor in stringText){
Console.WriteLine("{0} ", oneCharactor)// H e l l o W o r l d ! 가 출력된다.
}