C# 中的 Dictionary.Remove() 屬性用於從 Dictionary<TKey, TValue> 中刪除具有指定鍵的值。
用法
以下是語法 -
public bool Remove (TKey key);
上麵,關鍵是要刪除的關鍵。
示例
現在讓我們看一個實現 Dictionary.Remove() 屬性的示例 -
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Dictionary<string, string> dict =
new Dictionary<string, string>();
dict.Add("One", "Kagido");
dict.Add("Two", "Ngidi");
dict.Add("Three", "Devillers");
dict.Add("Four", "Smith");
dict.Add("Five", "Warner");
Console.WriteLine("Count of elements = "+dict.Count);
Console.WriteLine("Removing some keys...");
dict.Remove("Four");
dict.Remove("Five");
Console.WriteLine("Count of elements (updated) = "+dict.Count);
Console.WriteLine("\nKey/value pairs...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
Console.Write("\nAll the keys..\n");
Dictionary<string, string>.KeyCollection allKeys = dict.Keys;
foreach(string str in allKeys){
Console.WriteLine("Key = {0}", str);
}
}
}
輸出
這將產生以下輸出 -
Count of elements = 5 Removing some keys... Count of elements (updated) = 3 Key/value pairs... Key = One, Value = Kagido Key = Two, Value = Ngidi Key = Three, Value = Devillers All the keys.. Key = One Key = Two Key = Three
示例
現在讓我們看另一個例子來實現 Dictionary.Remove() 方法 -
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Dictionary<string, string> dict =
new Dictionary<string, string>();
dict.Add("One", "Kagido");
dict.Add("Two", "Ngidi");
dict.Add("Three", "Devillers");
dict.Add("Four", "Smith");
dict.Add("Five", "Warner");
Console.WriteLine("Count of elements = "+dict.Count);
Console.Write("\nAll the keys..\n");
Dictionary<string, string>.KeyCollection allKeys = dict.Keys;
foreach(string str in allKeys){
Console.WriteLine("Key = {0}", str);
}
Console.WriteLine("Removing some keys...");
dict.Remove("Four");
dict.Remove("Five");
Console.WriteLine("Count of elements (updated) = "+dict.Count);
Console.Write("\nAll the keys..updated\n");
foreach(string str in allKeys){
Console.WriteLine("Key = {0}", str);
}
Console.WriteLine("\nKey/value pairs...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
}
}
輸出
這將產生以下輸出 -
Count of elements = 5 All the keys.. Key = One Key = Two Key = Three Key = Four Key = Five Removing some keys... Count of elements (updated) = 3 All the keys..updated Key = One Key = Two Key = Three Key/value pairs... Key = One, Value = Kagido Key = Two, Value = Ngidi Key = Three, Value = Devillers
相關用法
- C# Dictionary.Remove用法及代碼示例
- C# Dictionary.Values用法及代碼示例
- C# Dictionary.Keys用法及代碼示例
- C# Dictionary.Add()用法及代碼示例
- C# Dictionary.ContainsValue()用法及代碼示例
- C# Dictionary.Clear用法及代碼示例
- C# Dictionary.Count用法及代碼示例
- C# Dictionary.Item[]用法及代碼示例
- C# Dictionary.ContainsKey()用法及代碼示例
- C# Decimal.FromOACurrency()用法及代碼示例
- C# DateTimeOffset.FromUnixTimeMilliseconds()用法及代碼示例
- C# DateTimeOffset.AddMinutes()用法及代碼示例
- C# Decimal.GetBits()用法及代碼示例
- C# DateTimeOffset.GetHashCode用法及代碼示例
- C# DateTime.ToFileTime()用法及代碼示例
- C# DateTimeOffset.ToLocalTime()用法及代碼示例
- C# DateTimeOffset.AddHours()用法及代碼示例
- C# DateTimeOffset.GetHashCode()用法及代碼示例
- C# Decimal.ToDouble()用法及代碼示例
- C# DateTimeOffset.FromUnixTimeSeconds()用法及代碼示例
注:本文由純淨天空篩選整理自AmitDiwan大神的英文原創作品 Dictionary.Remove() Method in C#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。