检查 SortedList 对象是否在 C# 中包含特定值
若要检查 SortedList 对象是否包含特定值,代码如下 −
示例
using System;
using System.Collections;
public class Demo {
public static void Main(){
SortedList list = new SortedList();
list.Add("1", "One");
list.Add("2", "Two");
list.Add("3", "Three");
list.Add("4", "Four");
list.Add("5", "Five");
list.Add("6", "Six");
list.Add("7", "Seven");
list.Add("8", "Eight");
Console.WriteLine("Key and Value of SortedList....");
foreach(DictionaryEntry k in list )
Console.WriteLine("Key: {0}, Value: {1}", k.Key , k.Value );
Console.WriteLine("Is the SortedList having the value? "+list.ContainsValue("Five"));
}
}输出
这会产生以下输出 −
Key and Value of SortedList.... Key: 1, Value: One Key: 2, Value: Two Key: 3, Value: Three Key: 4, Value: Four Key: 5, Value: Five Key: 6, Value: Six Key: 7, Value: Seven Key: 8, Value: Eight Is the SortedList having the value? True
示例
我们来看看另一个示例 −
using System;
using System.Collections;
public class Demo {
public static void Main(){
SortedList list = new SortedList();
list.Add("1", "One");
list.Add("2", "Two");
list.Add("3", "Three");
list.Add("4", "Four");
list.Add("5", "Five");
list.Add("6", "Six");
list.Add("7", "Seven");
list.Add("8", "Eight");
Console.WriteLine("Key and Value of SortedList....");
foreach(DictionaryEntry k in list )
Console.WriteLine("Key: {0}, Value: {1}", k.Key , k.Value );
Console.WriteLine("Is the SortedList having the value? "+list.ContainsValue("Tenth"));
}
}输出
这会产生以下输出 −
Key and Value of SortedList.... Key: 1, Value: One Key: 2, Value: Two Key: 3, Value: Three Key: 4, Value: Four Key: 5, Value: Five Key: 6, Value: Six Key: 7, Value: Seven Key: 8, Value: Eight Is the SortedList having the value? False
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP