C# 如何在C中的SortedDictionary中使用foreach查找下一个键#
我有一个排序字典,它将字符串存储为键和值。 我正在检查每个键的值,并带有一些条件C# 如何在C中的SortedDictionary中使用foreach查找下一个键#,c#,foreach,sorteddictionary,C#,Foreach,Sorteddictionary,我有一个排序字典,它将字符串存储为键和值。 我正在检查每个键的值,并带有一些条件 void sample() { SortedDictionary<string, string>.KeyCollection keyColl = clrDatesSelectedSortedDict.Keys; foreach (string key in keyColl) { if(chckstring = key) value = clrDatesSelected
void sample()
{
SortedDictionary<string, string>.KeyCollection keyColl = clrDatesSelectedSortedDict.Keys;
foreach (string key in keyColl)
{
if(chckstring = key)
value = clrDatesSelectedSortedDict[key];
}
}
void sample()
{
SortedDictionary.KeyCollection keyColl=CLRDATESelectedSortedDictionary.Keys;
foreach(keyColl中的字符串键)
{
如果(chckstring=key)
值=CLRDATESELECTEDSORTEDDICT[键];
}
}
实际上,如果我想知道下一把钥匙的话。如何获取下一个键值
还有一个问题,C#中的sortedDictionary是否可以使用for循环而不是foreach
请帮我上面的任何一个。
提前谢谢。像这样的东西怎么样
Public Sub TestLoopForeachKey()
Dim sd As New SortedDictionary(Of String, String)
For i = 0 To sd.Count
Dim nextIndex As Integer = i + 1
If nextIndex < sd.Count Then
' Exists another posterior key
Dim currentKey As String = sd(i)
Dim nextKey As String = sd(nextIndex)
End If
Next
End Sub
Public子测试loopforeachkey()
Dim sd作为新的SortedDictionary(字符串,字符串)
对于i=0到sd.Count
Dim nextIndex作为整数=i+1
如果nextIndex
当然可以,使用Ling扩展元素at():
void sample()
{
SortedDictionary.KeyCollection keyColl=CLRDATESelectedSortedDictionary.Keys;
对于(Int32 i=0;i
您尝试使用sd(i)
进行索引将不起作用,因为键是String
,而不是Integer
。是的,看起来我在单元测试中没有严格的选项,只是想展示一种方法;P
void sample()
{
SortedDictionary<String, String>.KeyCollection keyColl = clrDatesSelectedSortedDict.Keys;
for (Int32 i = 0; i < keyColl.Count; i++)
{
if (chckstring == keyColl.ElementAt(i).Key)
{
value = clrDatesSelectedSortedDict[keyColl.ElementAt(i).Key];
if ((i + 1) < keyColl.Count)
nextValue = clrDatesSelectedSortedDict[keyColl.ElementAt(i + 1).key];
}
}
}