C# 목록 색인
Harshit Jindal
2023년10월12일
Csharp
Csharp List

List<T>
는 동적 배열 또는 C#에 해당하는 C++ 벡터입니다. System.Collections.Generic
네임스페이스에 정의되어 있습니다.
다양한 유형의 데이터 모음을 저장하는 데 사용되며, 그런 다음 쉽게 액세스, 수정, 추가, 삭제 또는 정렬할 수 있습니다. 이 자습서에서는 C#에서 인덱스별로 목록 항목을 가져오는 방법을 살펴보겠습니다.
ElementAt()
메서드를 사용하여 C#
에서 인덱스별로 목록 항목 가져오기
using System;
using System.Linq;
using System.Collections.Generic;
public class Test {
public static void Main() {
var myList = new List<int> { 9, 3, 11, 23, 43 };
var firstItem = myList.ElementAt(0);
Console.WriteLine(firstItem);
}
}
출력:
9
ElementAt()
메서드는 인덱스를 인수로 사용하고 목록 내 해당 인덱스에 있는 값을 반환합니다. 위의 예에서는 샘플 사용 사례를 보여주었습니다.
List<T>.Item[Int32]
속성을 사용하여 C#
에서 인덱스별로 목록 항목 가져오기
using System;
using System.Linq;
using System.Collections.Generic;
public class Test {
public static void Main() {
var myList = new List<int> { 9, 3, 11, 23, 43 };
var firstItem = myList[0];
Console.WriteLine(firstItem);
}
}
출력:
9
위의 예는 매우 자명합니다. 여기에서 List<T>.Item[Int32]
속성을 사용하여 myList
목록의 첫 번째 요소에 액세스합니다. 제공된 인수를 0
에서 배열 크기 범위 내의 인덱스로 변경하여 모든 인덱스를 얻을 수 있습니다.
튜토리얼이 마음에 드시나요? DelftStack을 구독하세요 YouTube에서 저희가 더 많은 고품질 비디오 가이드를 제작할 수 있도록 지원해주세요. 구독하다
작가: Harshit Jindal
Harshit Jindal has done his Bachelors in Computer Science Engineering(2021) from DTU. He has always been a problem solver and now turned that into his profession. Currently working at M365 Cloud Security team(Torus) on Cloud Security Services and Datacenter Buildout Automation.
LinkedIn