这篇文章将讨论如何检查一个特定的值是否已经存在于一个 Dictionary<TKey,TValue> 在 C# 中。

1.使用 Dictionary<TKey,TValue>.ContainsValue() 方法

Dictionary<TKey,TValue> 类包含 ContainsValue() 方法,检查它是否包含特定值。它返回一个布尔值来指示该值是否被找到。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System ;
using System . Collections . Generic ;
public class Example
{
public static void Main ( )
{
Dictionary < string , string > dict = new Dictionary < string , string > ( ) ;
dict . Add ( "White" , "#FFFFFF" ) ;
dict . Add ( "Grey" , "#808080" ) ;
dict . Add ( "Silver" , "#C0C0C0" ) ;
dict . Add ( "Black" , "#000000" ) ;
boolean valueExists = dict . ContainsValue ( "#C0C0C0" ) ;
Console . WriteLine ( valueExists ) ; // True
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System ;
using System . Collections . Generic ;
public class Example
{
public static void Main ( )
{
Dictionary < string , string > dict = new Dictionary < string , string > ( ) ;
dict . Add ( "White" , "#FFFFFF" ) ;
dict . Add ( "Grey" , "#808080" ) ;
dict . Add ( "Silver" , "#C0C0C0" ) ;
dict . Add ( "Black" , "#000000" ) ;
boolean keyExists = dict . ContainsValue ( "Silver" ) ;
Console . WriteLine ( keyExists ) ; // True
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System ;
using System . Linq ;
using System . Collections . Generic ;
public static class Extensions
{
public static bool hasValue < K , V > ( this IDictionary < K , V > dict , V value )
{
return dict . Any ( kvp = > kvp . Value . Equals ( value ) ) ;
}
}
public class Example
{
public static void Main ( )
{
Dictionary < string , string > dict = new Dictionary < string , string > ( ) ;
dict . Add ( "White" , "#FFFFFF" ) ;
dict . Add ( "Grey" , "#808080" ) ;
dict . Add ( "Silver" , "#C0C0C0" ) ;
dict . Add ( "Black" , "#000000" ) ;
boolean valueExists = dict . hasValue ( "#C0C0C0" ) ;
Console . WriteLine ( valueExists ) ; // True
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System ;
using System . Collections . Generic ;
public static class Extensions
{
public static bool hasValue < K , V > ( this IDictionary < K , V > dict , V value )
{
foreach ( KeyValuePair < K , V > kvp in dict )
{
if ( kvp . Value . Equals ( value ) ) {
return true ;
}
}
return false ;
}
}
public class Example
{
public static void Main ( )
{
Dictionary < string , string > dict = new Dictionary < string , string > ( ) ;
dict . Add ( "White" , "#FFFFFF" ) ;
dict . Add ( "Grey" , "#808080" ) ;
dict . Add ( "Silver" , "#C0C0C0" ) ;
dict . Add ( "Black" , "#000000" ) ;
boolean valueExists = dict . hasValue ( "#C0C0C0" ) ;
Console . WriteLine ( valueExists ) ; // True
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System ;
using System . Collections . Generic ;
public static class Extensions
{
public static bool hasValue < K , V > ( this IDictionary < K , V > dict , V value )
{
foreach ( V v in dict . Values ) {
if ( v . Equals ( value ) ) {
return true ;
}
}
return false ;
}
}
public class Example
{
public static void Main ( )
{
Dictionary < string , string > dict = new Dictionary < string , string > ( ) ;
dict . Add ( "White" , "#FFFFFF" ) ;
dict . Add ( "Grey" , "#808080" ) ;
dict . Add ( "Silver" , "#C0C0C0" ) ;
dict . Add ( "Black" , "#000000" ) ;
boolean valueExists = dict . hasValue ( "#C0C0C0" ) ;
Console . WriteLine ( valueExists ) ; // True
}
}