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

C# में Dictionary.Count प्रॉपर्टी


C# में Dictionary.Count प्रॉपर्टी Dictionary में कुंजी/मान जोड़े की संख्या प्राप्त करती है।

सिंटैक्स

public int Count { get; }

आइए Dictionary.Count प्रॉपर्टी को लागू करने का एक उदाहरण देखते हैं -

उदाहरण

using System; 
using System.Collections.Generic; 
public class Demo { 
    public static void Main(){ 
        Dictionary<string, string> dict = new Dictionary<string, string>(); 
        dict.Add("One", "Chris"); 
        dict.Add("Two", "Steve"); 
        dict.Add("Three", "Messi"); 
        dict.Add("Four", "Ryan"); 
        dict.Add("Five", "Nathan"); 
        Console.WriteLine("Count of elements = "+dict.Count); 
        Console.WriteLine("\nKey/value pairs..."); 
        foreach(KeyValuePair<string, string> res in dict){ 
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value); 
        } 
        if (dict.ContainsValue("Angelina")) 
            Console.WriteLine("Value found!"); 
        else 
            Console.WriteLine("Value isn't in the dictionary!"); 
        dict.Clear(); 
        Console.WriteLine("Cleared Key/value pairs..."); 
        foreach(KeyValuePair<string, string> res in dict){ 
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value); 
        } 
        Console.WriteLine("Count of elements now = "+dict.Count); 
    } 
}

आउटपुट

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

Count of elements = 5 Key/value pairs... Key = One, Value = Chris Key = Two, Value = Steve Key = Three, Value = Messi Key = Four, Value = Ryan Key = Five, Value = Nathan Value isn't in the dictionary! Cleared Key/value pairs... Count of elements now = 0

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

उदाहरण

using System; 
using System.Collections.Generic; 
public class Demo { 
    public static void Main(){ 
        Dictionary<string, string> dict = new Dictionary<string, string>(); 
        dict.Add("One", "David"); 
        dict.Add("Two", "Brian"); 
        dict.Add("Three", "Paul"); 
        dict.Add("Four", "Ryan"); 
        dict.Add("Five", "Nathan"); 
        Console.WriteLine("Count of elements = "+dict.Count); 
    } 
}

आउटपुट

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

Count of elements = 5
  1. - पायथन में बिल्ट-इन डिक्शनरी फ़ंक्शन्स और मेथड्स

    पायथन में निम्नलिखित डिक्शनरी फ़ंक्शन्स शामिल हैं: क्र.सं. फ़ंक्शन और विवरण 1 cmp(dict1, dict2)दोनों डिक्ट के एलिमेंट्स की तुलना करता है। 2 len(dict)डिक्शनरी की कुल लंबाई देता है। यह डिक्शनरी में आइटम्स की संख्या के बराबर होगा। 3 str(dict)डिक्शनरी का प्रिंट करने योग्य स्ट्रिंग प्रतिनिधि

  1. - पायथन में डिक्शनरी मेथड: update(), has_key(), fromkeys()

    पायथन में डिक्शनरी सबसे अधिक उपयोग किए जाने वाले संग्रह डेटा प्रकार में से एक है। इसे कुंजी-मान जोड़े द्वारा दर्शाया जाता है। कुंजियाँ अनुक्रमित होती हैं लेकिन मान नहीं हो सकते। कई पायथन-बिल्ट-इन फ़ंक्शन हैं जो विभिन्न पायथन प्रोग्राम में डिक्शनरी का उपयोग करना बहुत आसान बनाते हैं। इस विषय में हम ती

  1. - नेस्टेड पायथन डिक्शनरी में एलिमेंट्स कैसे गिनें?

    डिक्शनरी में प्रत्येक की-वैल्यू पेयर पर निम्नलिखित एक्सप्रेशन द्वारा पुनरावृत्ति करना संभव है: for k, v in students.items(): चूंकि नेस्टेड पायथन डिक्शनरी में प्रत्येक आइटम का वैल्यू कंपोनेंट स्वयं एक डिक्शनरी है, प्रत्येक सब-डिक्शनरी की लंबाई len(v) है। लूप पर संचयी जोड़ करके सभी एलिमेंट्स की संख्