C#-Wörterbuch

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Freigeben: 2024-09-03 15:31:59
Original
1136 Leute haben es durchsucht

Die Dictionary-Klasse in C# wird als Dictionary dargestellt. Dabei handelt es sich um eine Sammlung ähnlich dem Wörterbuch im Englischen, die Wortsammlungen und deren Definitionen in alphabetischer Reihenfolge in mehr als einer Sprache enthält. Das Wörterbuch in C# besteht jedoch aus einer Sammlung von Schlüsseln und Werten, wobei der Schlüssel das Wort und der Wert die Definition darstellt. Dieses Wörterbuch Die Klasse in C# gehört zum System.Collection.Generics-Namespace und ist eine generische Sammlungsklasse, in der Tkey den Schlüsseltyp und Tvalue den Tvalue-Typ darstellt und die Variable von IDictionary Klasse oder Wörterbuch Die Klasse kann jedem Objekt im Wörterbuch zugewiesen werden.

Die Syntax der Dictionary-Klasse in C# lautet wie folgt:

IDictionary<TKey, TValue> variable_name = new Dictionary<TKey, TValue>();
Nach dem Login kopieren

oder

Dictionary<TKey, TValue > variable_name = new Dictionary<TKey, TValue >();
Nach dem Login kopieren

Arbeitsweise der Dictionary-Klasse in C#

  • Eine der wichtigen Klassen im System.Collections.Generic-Namespace ist Dictionary Klasse.
  • Eine generische Datenstruktur wird durch die Wörterbuchklasse in C# dargestellt, die Datenschlüssel und die entsprechenden Werte enthält. Daher können Daten jeglicher Art mithilfe der Instanz eines Wörterbuchs gespeichert werden.
  • Die Erweiterung der ICollection-Schnittstelle ist die IDictionary-Schnittstelle.
  • Die Add-Methode der Wörterbuchklasse wird verwendet, um die Objekte in der Instanz eines Wörterbuchs zu speichern.
  • Durch die Verwendung eines Wörterbuchs entfällt der Aufwand für das Ein- und Auspacken.

Betrachten Sie das folgende Beispiel, um die Verwendung der Dictionary-Klasse zu erläutern, um allein die Schlüssel zu erhalten:

using System;
using System.Collections.Generic;
//a class called program is defined
class program
{
// main method is called
public static void Main()
{
// a new dictionary is created with key type string and value type string
Dictionary<string, string> Dict = new Dictionary<string, string>();
// using add method in dictionary to add the objects to the dictionary
Dict.Add("A", "Karnataka");
Dict.Add("B", "Maharashtra");
Dict.Add("C", "Andra");
Dict.Add("D", "TamilNadu");
Dict.Add("E", "Delhi");
Dict.Add("F", "Goa");
// Finding the number of key value pairs in the dictionary
Console.WriteLine("The number of key value pairs in the dictionary are : " + Dict.Count);
// using the property of keys to get the keys alone from the dictionary
Dictionary<string, string>.KeyCollection key =  Dict.Keys;
// a foreach loop is used to loop around every key in the dictionary and to obtain each key value
foreach(string sh in key)
{
Console.WriteLine("The key is referred as = {0}", sh);
}
}
}
Nach dem Login kopieren

Die Ausgabe des obigen Programms ist wie im Schnappschuss unten dargestellt:

C#-Wörterbuch

Im obigen Programm ist das Programm die definierte Klasse. Dann wird die Hauptmethode aufgerufen. Es wird ein neues Wörterbuch mit dem Schlüsseltyp string und dem Werttyp string erstellt. Verwenden Sie dann die Add-Methode im Wörterbuch, um die Objekte zum Wörterbuch hinzuzufügen. Anschließend wird mithilfe der Zählung die Anzahl der Schlüssel-Wert-Paare im Wörterbuch ermittelt. Mithilfe der Schlüsseleigenschaft werden dann nur die Schlüssel aus dem Wörterbuch extrahiert. Dann wird eine foreach-Schleife verwendet, um jeden Schlüssel im Wörterbuch zu durchlaufen und jeden Schlüsselwert zu erhalten. Die Ausgabe des Programms ist wie im Schnappschuss oben dargestellt.

Methoden des C#-Wörterbuchs

Es gibt mehrere Methoden in der Dictionary-Klasse in C#. Sie sind:

1. Add()

  • Die Methode add() wird verwendet, um ein Element zur Sammlung des Wörterbuchs hinzuzufügen.
  • Die Methode add() wird verwendet, um Schlüssel-Wert-Paare zur Sammlung von Dictionary hinzuzufügen.
  • Betrachten Sie das folgende Beispiel, um die Add-Methode der Wörterbuchklasse zu demonstrieren:
using System;
using System.Collections.Generic;
//a class called check is defined
public class Check
{
//main method is called
public static void Main()
{
//a new dictionary is created with key type int and value type string
IDictionary<int, string> str = new Dictionary<int, string>();
//Add method is used to add objects to the dictionary
str.Add(3,"Green");
str.Add(1,"Saffron");
str.Add(2,"White");
str.Add(new KeyValuePair<int, string>(4, "Blue"));
//The number of objects in the dictionary is displayed using count
Console.WriteLine("The number of objects in the given dictionary is: {0} ", str.Count);
}
}
Nach dem Login kopieren

Die Ausgabe des obigen Programms ist wie im Schnappschuss unten dargestellt:

C#-Wörterbuch

2.  Remove()

Die Remove()-Methode wird verwendet, um das erste Vorkommen des angegebenen Elements aus dem Wörterbuch zu entfernen.

Die Methode „remove()“ wird verwendet, um ein Element, das zusammen mit dem Schlüssel angegeben wird, aus dem Wörterbuchobjekt zu entfernen.

Betrachten Sie das folgende Beispiel, um die Verwendung der Remove()-Methode in der Dictionary-Klasse zu demonstrieren:

using System;
using System.Collections.Generic;
//a class called check1 is defined
public class Check1
{
//main method is called
public static void Main()
{
//a new dictionary is created with key type int and value type string
IDictionary<int, string> str1 = new Dictionary<int, string>();
//Add method is used to add objects to the dictionary
str1.Add(3,"Green");
str1.Add(1,"Saffron");
str1.Add(2,"White");
str1.Add(new KeyValuePair<int, string>(4, "Blue"));
str1.Remove(1);
str1.Remove(new KeyValuePair<int, string>(2, "White"));
//The number of objects in the dictionary is displayed using count
Console.WriteLine("The number of objects in the given dictionary is: {0} ", str1.Count);
}
}
Nach dem Login kopieren

Die Ausgabe des obigen Programms ist wie im Schnappschuss unten dargestellt:

C#-Wörterbuch

3. EnthältKey()

Die Methode ContainsKey() wird verwendet, um zu prüfen, ob der angegebene Schlüssel in Dictionary vorhanden ist.

Betrachten Sie das folgende Programm, um die Verwendung der Methode „ContainsKey()“ in der Klasse „Dictionary“ zu demonstrieren:

using System;
using System.Collections.Generic;
//a class called2 check is defined
public class Check2
{
//main method is called
public static void Main()
{
//a new dictionary is created with key type int and value type string
IDictionary<int, string> str2 = new Dictionary<int, string>();
//Add method is used to add objects to the dictionary
str2.Add(3,"Green");
str2.Add(1,"Saffron");
str2.Add(2,"White");
str2.Add(new KeyValuePair<int, string>(4, "Blue"));
str2.Remove(1);
str2.Remove(new KeyValuePair<int, string>(2, "White"));
Console.WriteLine("If the key 3 is present?{0}", str2.ContainsKey(3));
Console.WriteLine("If the key 2 is present? {0}", str2.Contains(new KeyValuePair<int, string>(2, "White")));
//The number of objects in the dictionary is displayed using count
Console.WriteLine("The number of objects in the given dictionary is: {0} ", str2.Count);
}
}
Nach dem Login kopieren

Die Ausgabe des obigen Programms ist wie im Schnappschuss unten dargestellt:

C#-Wörterbuch

4. EnthältValue()

Mit der Methode ContainsValue() wird überprüft, ob der angegebene Wert in Dictionary vorhanden ist.

Betrachten Sie das folgende Programm, um die Verwendung der Methode „ContainsValue()“ in der Klasse „Dictionary“ zu demonstrieren:

using System;
using System.Collections.Generic;
//a class called check3 is defined
public class Check3
{
//main method is called
public static void Main()
{
//a new dictionary is created with key type int and value type string
IDictionary<int, string> str2 = new Dictionary<int, string>();
//Add method is used to add objects to the dictionary
str2.Add(3,"Green");
str2.Add(1,"Saffron");
str2.Add(2,"White");
str2.Add(new KeyValuePair<int, string>(4, "Blue"));
str2.Remove(1);
str2.Remove(new KeyValuePair<int, string>(2, "White"));
Console.WriteLine("If the key 3 is present?{0}", str2.ContainsKey(3));
Console.WriteLine("If the key 2 is present? {0}", str2.Contains(new KeyValuePair<int, string>(2, "White")));
//new dictionary of both string key and string value types is defined
Dictionary<string, string> stri = new Dictionary<string, string>();
stri.Add("Flag","Green");
Console.WriteLine("If the value Green is present?{0}", stri.ContainsValue("Green"));
//The number of objects in the dictionary is displayed using count
Console.WriteLine("The number of objects in the given dictionary is: {0} ", str2.Count);
}
}
Nach dem Login kopieren

Die Ausgabe des obigen Programms ist wie im Schnappschuss unten dargestellt:

C#-Wörterbuch

5. Clear()

Die Methode clear() wird verwendet, um alle Objekte in der Wörterbuchklasse zu löschen.

Betrachten Sie das folgende Programm, um die Verwendung der Methode „ContainsValue()“ in der Klasse „Dictionary“ zu demonstrieren:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
//a class called check4 is defined
public class Check4
{
//main method is called
public static void Main()
{
//a new dictionary is created with key type int and value type string
IDictionary<int, string> str3 = new Dictionary<int, string>();
//Add method is used to add objects to the dictionary
str3.Add(3,"Green");
str3.Add(1,"Saffron");
str3.Add(2,"White");
str3.Add(new KeyValuePair<int, string>(4, "Blue"));
str3.Remove(1);
str3.Remove(new KeyValuePair<int, string>(2, "White"));
Console.WriteLine("If the key 3 is present?{0}", str3.ContainsKey(3));
Console.WriteLine("If the key 2 is present? {0}", str3.Contains(new KeyValuePair<int, string>(2, "White")));
//new dictionary of both string key and string value types is defined
Dictionary<string, string> stri1 = new Dictionary<string, string>();
stri1.Add("Flag","Green");
Console.WriteLine("If the value Green is present?{0}", stri1.ContainsValue("Green"));
//The number of objects in the dictionary is displayed using count
Console.WriteLine("The number of objects in the given dictionary is: {0} ", str3.Count);
Console.Clear();
}
}
Nach dem Login kopieren

Die Ausgabe des obigen Programms ist leer, wie im Schnappschuss unten gezeigt:

C#-Wörterbuch

6. TryGetValue()

Die TryGetValue()-Methode prüft, ob der angegebene Schlüssel existiert. Wenn er nicht existiert, gibt sie false zurück. Wenn der angegebene Schlüssel existiert, gibt er true zurück und weist den angegebenen Wert dem angegebenen Schlüssel zu.

Consider the below program to demonstrate the usage of TryGetValue() method of Dictionary class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
//a class called check4 is defined
public class Check4
{
//main method is called
public static void Main()
{
//a new dictionary is created with key type int and value type string
IDictionary<int, string> str3 = new Dictionary<int, string>();
//Add method is used to add objects to the dictionary
str3.Add(3,"Green");
str3.Add(1,"Saffron");
str3.Add(2,"White");
str3.Add(new KeyValuePair<int, string>(4, "Blue"));
str3.Remove(1);
str3.Remove(new KeyValuePair<int, string>(2, "White"));
Console.WriteLine("If the key 3 is present?{0}", str3.ContainsKey(3));
Console.WriteLine("If the key 2 is present? {0}", str3.Contains(new KeyValuePair<int, string>(2, "White")));
//new dictionary of both string key and string value types is defined
Dictionary<string, string> stri1 = new Dictionary<string, string>();
stri1.Add("Flag","Green");
Console.WriteLine("If the value Green is present?{0}", stri1.ContainsValue("Green"));
string res;
if(str3.TryGetValue(4, out res))
{
Console.WriteLine("The value of the specified key is {0}", res);
}
else
{
Console.WriteLine("The specified key is not present.");
}
//The number of objects in the dictionary is displayed using count
Console.WriteLine("The number of objects in the given dictionary is: {0} ", str3.Count);
}
}
Nach dem Login kopieren

The output of the above program is as shown in the snapshot below:

C#-Wörterbuch

Conclusion

In this tutorial, we understand the concept of Dictionary class in C# through definition, the syntax of Dictionary class in C#, working of Dictionary class, and their methods through programming examples and their outputs.

Das obige ist der detaillierte Inhalt vonC#-Wörterbuch. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage