C#
 Computer >> コンピューター >  >> プログラミング >> C#

Dictionary にC#で特定の値が含まれているかどうかを確認します


Dictionary に特定の値が含まれているかどうかを確認するためのコードは、次のとおりです-

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("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("Kevin"))
         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 = John
Key = Two, Value = Tom
Key = Three, Value = Jacob
Key = Four, Value = Kevin
Key = Five, Value = Nathan
Value found!
Cleared Key/value pairs...
Count of elements now = 0

別の例を見てみましょう-

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

  1. 入力された番号がアームストロング番号であるかどうかを確認するC#プログラム

    アームストロング数の場合、数が3桁であるとすると、その桁の3乗の合計はその数自体と等しくなります。 たとえば、153は-に等しい 1³ + 3³ + 5³ C#を使用してチェックするには、値をチェックして残りを見つけます。ここで、「val」はアームストロングをチェックする番号です- for (int i = val; i > 0; i = i / 10) {    rem = i % 10;    sum = sum + rem*rem*rem; } 次に、加算を実際の値と比較します。一致する場合、それは立方体の合

  2. PythonPandas-DateOffset値が正規化されているかどうかを確認します

    DateOff設定値が正規化されているかどうかを確認するには、Pandasのoffset.normalizeプロパティを使用します。 まず、必要なライブラリをインポートします- from pandas.tseries.offsets import DateOffset import pandas as pd パンダでタイムスタンプオブジェクトを設定します- timestamp = pd.Timestamp('2021-09-26 03:25:02.000045') DateOffsetを作成します。 「months」パラメータを使用して、ここで月をインクリメントします。