कंटेन्सकी सी # में एक डिक्शनरी विधि है और जांचें कि डिक्शनरी में कोई कुंजी मौजूद है या नहीं।
एक शब्दकोश घोषित करें और तत्व जोड़ें -
var dict = new Dictionary<string, int>() {
{"TV", 1},
{"Home Theatre", 2},
{"Amazon Alexa", 3},
{"Google Home", 5},
{"Laptop", 5},
{"Bluetooth Speaker", 6}
}; अब, मान लें कि आपको डिक्शनरी में किसी विशेष तत्व के अस्तित्व की जांच करने की आवश्यकता है। उसके लिए, ContainKey() विधि का उपयोग करें -
if (dict.ContainsKey("Laptop") == true) {
Console.WriteLine(dict["Laptop"]);
} निम्नलिखित कोड है -
उदाहरण
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
var dict = new Dictionary<string, int>() {
{"TV", 1},
{"Home Theatre", 2},
{"Amazon Alexa", 3},
{"Google Home", 5},
{"Laptop", 5},
{"Bluetooth Speaker", 6}
};
if (dict.ContainsKey("Laptop") == true) {
Console.WriteLine(dict["Laptop"]);
}
if (dict.ContainsKey("Amazon Alexa") == true) {
Console.WriteLine(dict["Amazon Alexa"]);
}
}
} आउटपुट
5 3