簡體   English   中英

如何在.Net 2.0中查找List中字符串的索引

[英]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

因為它是謂詞,所以你應該插入一個lambda函數。

FindIndex(x => x == "HKG")

請參閱: http//www.dotnetperls.com/lambda

使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM