[英]How to find an index of a string within a List in .Net 2.0
我試圖獲取List中字符串的索引號。 我嘗試了以下代碼:
List<string> Markets = new List<string>() {"HKG", "TYO", "NSE", "STU", "FRA", "LON", "SIN", "MIL", "TSE", "ASX", "STO", "AEX", "MEX", "NSE", "EPA", "SWX", "CVE", "BRU", "SWX"};
int index = Markets.FindIndex("HKG");
出現以下錯誤:
'System.Collections.Generic.List.FindIndex(System.Predicate <string>)'的最佳重載方法匹配有一些無效參數參數1:無法從'string'轉換為'System.Predicate <string>'
知道如何讓編譯器喜歡這段代碼嗎?
PS我使用的是QuantShare的C#,應該是.Net 2.0
使用IndexOf
int index = Markets.IndexOf("HKG");
使用FindIndex:
List<string> Markets = new List<string>() { "HKG", "TYO", "NSE", "STU", "FRA", "LON", "SIN", "MIL", "TSE", "ASX", "STO", "AEX", "MEX", "NSE", "EPA", "SWX", "CVE", "BRU", "SWX" };
var index = Markets.FindIndex(a => a == "HKG");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.