C# Dictionary.Keys用法及代码示例

摘要:
此属性用于获取包含Dictionary中的键的集合。以下示例程序旨在说明上面讨论的属性的使用:范例1://C#codetogetthekeys//intheDictionaryusingSystem;usingSystem.Collections.Generic;classGFG{//DrivercodepublicstaticvoidMain(){//Createanewdictionaryof//strings,withstringkeys.DictionarymyDict=newDictionary();//Addingkey/valuepairsinmyDictmyDict.Add;myDict.Add;myDict.Add;myDict.Add;myDict.Add;myDict.Add;//Togetcountofkey/valuepairsinmyDictConsole.WriteLine;//Togetthekeysalone,//usetheKeysproperty.Dictionary.KeyCollectionkeyColl=myDict.Keys;//TheelementsoftheKeyCollection//arestronglytypedwiththetype//thatwasspecifiedfordictionary//keysforeach{Console.WriteLine;}}}输出:Totalkey/valuepairsinmyDictare:6Key=AustraliaKey=BelgiumKey=NetherlandsKey=ChinaKey=RussiaKey=India范例2://C#codetogetthekeysintheDictionaryusingSystem;usingSystem.Collections.Generic;classGFG{//DrivercodepublicstaticvoidMain(){//Createanewdictionaryof//strings,withstringkeys.DictionarymyDict=newDictionary();//Addingkey/valuepairsinmyDictmyDict.Add(9,8);myDict.Add(3,4);myDict.Add(4,7);myDict.Add(1,7);//Togetcountofkey/valuepairsinmyDictConsole.WriteLine;//Togetthekeysalone,//usetheKeysproperty.Dictionary.KeyCollectionkeyColl=myDict.Keys;//TheelementsoftheKeyCollection//arestronglytypedwiththetype//thatwasspecifiedfordictionarykeys.foreach{Console.WriteLine;}}}输出:Totalkey/valuepairsinmyDictare:4Key=9Key=3Key=4Key=1Key就是键,value就是值,我们在很多地方都会用到字典,他的特点就是查找很快,当然比List快。Clear从Dictionary中移除所有的键和值。ContainsValue确定Dictionary是否包含特定值。)GetEnumerator返回循环访问Dictionary的枚举器。)GetObjectData实现System.Runtime.Serialization.ISerializable接口,并返回序列化Dictionary实例所需的数据。)OnDeserialization实现System.Runtime.Serialization.ISerializable接口,并在完成反序列化之后引发反序列化事件。ToString返回表示当前对象的字符串。

此属性用于获取包含Dictionary中的键的集合。

用法:

public System.Collections.Generic.Dictionary<TKey, TValue>.KeyCollection Keys { get; }


返回值:它返回一个包含Dictionary中关键字的集合。

以下示例程序旨在说明上面讨论的属性的使用:

范例1:

// C# code to get the keys 
// in the Dictionary 
using System; 
using System.Collections.Generic; 

class GFG { 

// Driver code 
public static void Main() 
{ 

// Create a new dictionary of 
// strings, with string keys. 
Dictionary<string, string> myDict = 
new Dictionary<string, string>(); 

// Adding key/value pairs in myDict 
myDict.Add("Australia", "Canberra"); 
myDict.Add("Belgium", "Brussels"); 
myDict.Add("Netherlands", "Amsterdam"); 
myDict.Add("China", "Beijing"); 
myDict.Add("Russia", "Moscow"); 
myDict.Add("India", "New Delhi"); 

// To get count of key/value pairs in myDict 
Console.WriteLine("Total key/value pairs"+ 
" in myDict are:" + myDict.Count); 

// To get the keys alone, 
// use the Keysproperty. 
Dictionary<string, string>.KeyCollection keyColl = 
myDict.Keys; 

// The elements of the KeyCollection 
// are strongly typed with the type 
// that was specified for dictionary 
// keys 
foreach(string s in keyColl) 
{ 
Console.WriteLine("Key = {0}", s); 
} 
} 
}
输出:
Total key/value pairs in myDict are:6
Key = Australia
Key = Belgium
Key = Netherlands
Key = China
Key = Russia
Key = India

范例2:

// C# code to get the keys in the Dictionary 
using System; 
using System.Collections.Generic; 

class GFG { 

// Driver code 
public static void Main() 
{ 

// Create a new dictionary of 
// strings, with string keys. 
Dictionary<int, int> myDict = 
new Dictionary<int, int>(); 

// Adding key/value pairs in myDict 
myDict.Add(9, 8); 
myDict.Add(3, 4); 
myDict.Add(4, 7); 
myDict.Add(1, 7); 

// To get count of key/value pairs in myDict 
Console.WriteLine("Total key/value pairs "+ 
"in myDict are:" + myDict.Count); 

// To get the keys alone, 
// use the Keysproperty. 
Dictionary<int, int>.KeyCollection keyColl = 
myDict.Keys; 

// The elements of the KeyCollection 
// are strongly typed with the type 
// that was specified for dictionary keys. 
foreach(int s in keyColl) 
{ 
Console.WriteLine("Key = {0}", s); 
} 
} 
}
输出:
Total key/value pairs in myDict are:4
Key = 9
Key = 3
Key = 4
Key = 1
Key就是键,value就是值,

我们在很多地方都会用到字典,他的特点就是查找很快,当然比List快。

字典必须包含名空间System.Collection.Generic

Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)

键必须是唯一的,而值不需要唯一的
键和值都可以是任何类型(比如:string, int, 自定义类型,等等)

常用属性

名称 说明
Comparer 获取用于确定字典中的键是否相等的 IEqualityComparer<T>。
Count 获取包含在 Dictionary<TKey, TValue> 中的键/值对的数目。
Item 获取或设置与指定的键相关联的值。
Keys 获取包含 Dictionary<TKey, TValue> 中的键的集合。
Values 获取包含 Dictionary<TKey, TValue> 中的值的集合。

常用方法
名称 说明
Add 将指定的键和值添加到字典中。
Clear 从 Dictionary<TKey, TValue> 中移除所有的键和值。
ContainsKey 确定 Dictionary<TKey, TValue> 是否包含指定的键。
ContainsValue 确定 Dictionary<TKey, TValue> 是否包含特定值。
Equals(Object) 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)
Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
GetEnumerator 返回循环访问 Dictionary<TKey, TValue> 的枚举器。
GetHashCode 用作特定类型的哈希函数。 (继承自 Object。)
GetObjectData 实现 System.Runtime.Serialization.ISerializable 接口,并返回序列化 Dictionary<TKey, TValue> 实例所需的数据。
GetType 获取当前实例的 Type。 (继承自 Object。)
MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
OnDeserialization 实现 System.Runtime.Serialization.ISerializable 接口,并在完成反序列化之后引发反序列化事件。
Remove 从 Dictionary<TKey, TValue> 中移除所指定的键的值。
ToString 返回表示当前对象的字符串。 (继承自 Object。)
TryGetValue 获取与指定的键相关联的值。

免责声明:文章转载自《C# Dictionary.Keys用法及代码示例》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Oracle存储过程记录异常日志安装Nginx并为node.js设置反向代理下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

dbus

dbus 概念 --翻遍了google和百度,就这个图对入门帮助最大,dbus的文档真难找 fcitx 概念 dbus-send 命令行 /inputmethod --对象路径 org.freedesktop.DBus.Properties --interface --dest=org.fcitx.Fcitx //名称 dbus-send -...

面向对象基础——索引器

  C#中的string是可以通过索引器来访问对象中的字符,但却不能修改字符的值。   我们来看string中关于索引器的定义,如下图。   上图中索引器如同属性一样,具有get方法,却没有set方法,所以这就是为什么C#中的string类型的变量都是只读的。      现在让我们来编写属于自己的索引器: 1 class Program 2...

【转帖】C# DllImport 系统调用使用详解 托管代码的介绍 EntryPoint的使用

1 DLLImport的使用 using System; using System.Runtime.InteropServices; //命名空间 class Example { //用DllImport 导入Win32的MessageBox函数 [DllImport("user32.dll", CharSet = CharSet.Unicode)] p...

接口与委托

在接口中可以声明方法、属性、索引指示器和事件,接口中并不提供它们的实现。因此接口是函数成员声明的集合。如果类或结构从一个接口派生,则这个类或结构负责实现该接口中所声明的所有函数成员。一个接口可以继承多个接口,而一个类或结构可以实现多个接口。由于C#语言不支持多继承,因此,如果某个类需要继承多个类的行为时,只能使用多个接口加以说明。 委托类型,在功能上它类...

spring装配bean的三种方式及其混合装配

在spring容器中装配bean有三种基本方式和混合装配方式: 隐式的bean自动发现机制和自动装配 在java中进行显式配置 在xml中配置 混合装配(在多个java文件中配置、在JavaConfig中引用XML配置、在XML中引用JavaConfig配置) 一、使用自动化方式装配bean示例: 1:创建maven项目并引入依赖: <?xml...

Java调用Http/Https接口(8,end)OkHttp调用Http/Https接口

OkHttp是一个高效的HTTP客户端,在Android中用的比较多,也可以用在Java中;本文主要介绍OkHttp在java中的使用,文中所使用到的软件版本:Java 1.8.0_191、SpringBoot2.2.1.RELEASE。 1、OkHttp特点 a、支持HTTP/2,允许所有同一个主机地址的请求共享同一个socket连接b、连接池减少请求延...