반응형
문자열 목록을 초기화하는 방법 (목록문자열 값이 많은)
문자열 목록을 어떻게 (C # 이니셜 라이저로) 초기화 할 수 있습니까? 아래 예제를 시도했지만 작동하지 않습니다.
List<string> optionList = new List<string>
{
"AdditionalCardPersonAddressType","AutomaticRaiseCreditLimit","CardDeliveryTimeWeekDay"
}();
List<string> mylist = new List<string>(new string[] { "element1", "element2", "element3" });
()
마지막에 제거하십시오 .
List<string> optionList = new List<string>
{ "AdditionalCardPersonAdressType", /* rest of elements */ };
실제로 질문하지는 않았지만 코드는
List<string> optionList = new List<string> { "string1", "string2", ..., "stringN"};
즉, 목록 다음에 후행 ()이 없습니다.
당신의 함수는 괜찮지 만 ()
last를 뒤에 넣었 기 때문에 작동하지 않습니다 }
. 오류 ()
바로 옆의 맨 위로 이동하면 new List<string>()
중지됩니다.
아래 샘플 :
List<string> optionList = new List<string>()
{
"AdditionalCardPersonAdressType","AutomaticRaiseCreditLimit","CardDeliveryTimeWeekDay"
};
var animals = new List<string> { "bird", "dog" };
List<string> animals= new List<string> { "bird", "dog" };
위의 두 가지 방법 중 가장 짧은 방법은 https://www.dotnetperls.com/list 를 참조 하십시오.
이것은 초기화 방법이며 더 동적으로 만들고 싶다면 List.Add ()를 사용할 수 있습니다.
List<string> optionList = new List<string> {"AdditionalCardPersonAdressType"};
optionList.Add("AutomaticRaiseCreditLimit");
optionList.Add("CardDeliveryTimeWeekDay");
이런 식으로 IO에서 값을 가져 오는 경우 동적으로 할당 된 목록에 추가 할 수 있습니다.
다음과 같이 둥근 괄호를 이동하십시오.
var optionList = new List<string>(){"AdditionalCardPersonAdressType","AutomaticRaiseCreditLimit","CardDeliveryTimeWeekDay"};
List<string> animals= new List<string>();
animals.Add("dog");
animals.Add("tiger");
이것이 당신이하는 방법입니다.
List <string> list1 = new List <string>();
추가하는 것을 잊지 마십시오
using System.Collections.Generic;
반응형
'Programing' 카테고리의 다른 글
C #에서 문자열 암호화 및 암호 해독 (0) | 2020.03.06 |
---|---|
이름으로 쿠키 받기 (0) | 2020.03.06 |
C # 또는 .NET에서 가장 이상한 경우는 무엇입니까? (0) | 2020.03.06 |
Android SDK 빌드 도구, 플랫폼 도구 및 도구 란 무엇입니까? (0) | 2020.03.06 |
REST 웹 애플리케이션의 페이지 매김 (0) | 2020.03.06 |