C# | Check if the StringDictionary contains a specific value
Last Updated :
01 Feb, 2019
StringDictionary.ContainsValue(String) method is used to check whether the StringDictionary contains a specific value or not.
Syntax:
public virtual bool ContainsValue (string value);
Here, value is the value to locate in the StringDictionary. The value can be null.
Return Value: The method returns true if the StringDictionary contains an element with the specified value, otherwise it returns false.
Below programs illustrate the use of StringDictionary.ContainsValue(String) method:
Example 1:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
StringDictionary myDict = new StringDictionary();
myDict.Add( "G" , "Geeks" );
myDict.Add( "F" , "For" );
myDict.Add( "C" , "C++" );
myDict.Add( "DS" , "Data Structures" );
myDict.Add( "N" , "Noida" );
if (myDict.ContainsValue( "C++" ))
Console.WriteLine( "StringDictionary myDict contains the value" );
else
Console.WriteLine( "StringDictionary myDict does not contain the value" );
}
}
|
Output:
StringDictionary myDict contains the value
Example 2:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
StringDictionary myDict = new StringDictionary();
myDict.Add( "G" , "Geeks" );
myDict.Add( "F" , "For" );
myDict.Add( "C" , "C++" );
myDict.Add( "DS" , "Data Structures" );
myDict.Add( "N" , "Noida" );
if (myDict.ContainsValue( null ))
Console.WriteLine( "StringDictionary myDict contains the value" );
else
Console.WriteLine( "StringDictionary myDict does not contain the value" );
}
}
|
Output:
StringDictionary myDict does not contain the value
Note:
- The values of the elements of the StringDictionary are compared to the specified value using the Object.Equals method.
- This method is an O(n) operation, where n is Count.
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...