Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में Dictionary.ContainsKey() मेथड


C# में Dictionary.ContainsKey() मेथड जांचता है कि Dictionary<TKey, TValue> में निर्दिष्ट कुंजी है या नहीं।

सिंटैक्स

public bool ContainsKey (TKey key);

ऊपर, पैरामीटर key वह कुंजी है जिसे ढूंढा जाना है।

आइए अब Dictionary.ContainsKey() मेथड को लागू करने का एक उदाहरण देखें -

उदाहरण

using System; 
using System.Collections.Generic; 
public class Demo { 
    public static void Main(){ 
        Dictionary<string, string> dict = new Dictionary<string, string>(); 
        dict.Add("One", "John"); 
        dict.Add("Two", "Tom"); 
        dict.Add("Three", "Jacob"); 
        dict.Add("Four", "Kevin"); 
        dict.Add("Five", "Nathan"); 
        Console.WriteLine("तत्वों की संख्या = "+dict.Count); 
        Console.WriteLine("\nकुंजी/मान जोड़े..."); 
        foreach(KeyValuePair<string, string> res in dict){ 
            Console.WriteLine("कुंजी = {0}, मान = {1}", res.Key, res.Value); 
        } 
        if (dict.ContainsKey("Three")) 
            Console.WriteLine("कुंजी मिली!"); 
        else 
            Console.WriteLine("कुंजी डिक्शनरी में नहीं है!"); 
        dict.Clear(); 
        Console.WriteLine("साफ़ किए गए कुंजी/मान जोड़े..."); 
        foreach(KeyValuePair<string, string> res in dict){ 
            Console.WriteLine("कुंजी = {0}, मान = {1}", res.Key, res.Value); 
        } 
        Console.WriteLine("अब तत्वों की संख्या = "+dict.Count); 
    } 
}

आउटपुट

यह निम्नलिखित आउटपुट देगा -

तत्वों की संख्या = 5 
कुंजी/मान जोड़े... 
कुंजी = One, मान = John 
कुंजी = Two, मान = Tom 
कुंजी = Three, मान = Jacob 
कुंजी = Four, मान = Kevin 
कुंजी = Five, मान = Nathan 
कुंजी मिली! 
साफ़ किए गए कुंजी/मान जोड़े... 
अब तत्वों की संख्या = 0

आइए अब Dictionary.ContainsKey() प्रॉपर्टी को लागू करने का एक और उदाहरण देखें -

उदाहरण

using System; 
using System.Collections.Generic; 
public class Demo { 
    public static void Main(){ 
        Dictionary<string, string> dict = new Dictionary<string, string>(); 
        dict.Add("One", "John"); 
        dict.Add("Two", "Tom"); 
        dict.Add("Three", "Jacob"); 
        dict.Add("Four", "Kevin"); 
        dict.Add("Five", "Nathan"); 
        Console.WriteLine("तत्वों की संख्या = "+dict.Count); 
        Console.WriteLine("\nकुंजी/मान जोड़े..."); 
        foreach(KeyValuePair<string, string> res in dict){ 
            Console.WriteLine("कुंजी = {0}, मान = {1}", res.Key, res.Value); 
        } 
        if (dict.ContainsKey("mykey")) 
            Console.WriteLine("कुंजी मिली!"); 
        else 
            Console.WriteLine("कुंजी डिक्शनरी में नहीं है!"); 
        dict.Clear(); 
        Console.WriteLine("साफ़ किए गए कुंजी/मान जोड़े..."); 
        foreach(KeyValuePair<string, string> res in dict){ 
            Console.WriteLine("कुंजी = {0}, मान = {1}", res.Key, res.Value); 
        } 
        Console.WriteLine("अब तत्वों की संख्या = "+dict.Count); 
    } 
}

आउटपुट

यह निम्नलिखित आउटपुट देगा -

तत्वों की संख्या = 5 
कुंजी/मान जोड़े... 
कुंजी = One, मान = John 
कुंजी = Two, मान = Tom 
कुंजी = Three, मान = Jacob 
कुंजी = Four, मान = Kevin 
कुंजी = Five, मान = Nathan 
कुंजी डिक्शनरी में नहीं है! 
साफ़ किए गए कुंजी/मान जोड़े... 
अब तत्वों की संख्या = 0

  1. - Node.js में crypto.generateKeyPairSync() मेथड

    Node.js में crypto.generateKeyPairSync() मेथड का उपयोग निर्दिष्ट प्रकार के नए असममित की जोड़ी (asymmetric key pair) को सिंक्रोनस (sync) फ्लो में जेनरेट करने के लिए किया जाता है। की जोड़ी जेनरेट करने के लिए समर्थित प्रकार हैं: RSA, DSA, EC, Ed25519, Ed448, X25519, X448 और DH। जब publicKeyEncoding या

  1. - Node.js में crypto.generateKeyPair() मेथड

    Node.js में crypto.generateKeyPair() मेथड का उपयोग निर्दिष्ट प्रकार के नए असममित की जोड़ी (asymmetric key pair) को जेनरेट करने के लिए किया जाता है। की जोड़ी जेनरेट करने के लिए समर्थित प्रकार हैं: RSA, DSA, EC, Ed25519, Ed448, X25519, X448 और DH। जब publicKeyEncoding या privateKeyEncoding निर्दिष्ट

  1. HTML DOM Storage key() मेथड HTML DOM Storage key() मेथड

    HTML DOM Storage key() मेथड का उपयोग स्टोरेज ऑब्जेक्ट में दिए गए इंडेक्स पर की नाम को वापस करने के लिए किया जाता है। इंडेक्स को key() मेथड के पैरामीटर के रूप में पास किया जाता है। स्टोरेज ऑब्जेक्ट सेशन ऑब्जेक्ट या localStorage ऑब्जेक्ट हो सकता है। सिंटैक्स LocalStorage का उपयोग करके Storage key() म