C# KeyValuePair<TKey,TValue>的用法
命名空间:System.Collections.Generic
构造函数:public KeyValuePair (TKey key, TValue value);
属性:只读属性 Key ,只读属性 Value
方法:public void Deconstruct (out TKey key, out TValue value);方法 解构可以做模式匹配
public override string ToString (); 字符串表示形式,它包括键和值的字符串表示形式。
初始化,由于Key 、Value是只读属性,所以不能采用初始值设定项初始化,只能用构造函数初始化:
var kvp =new KeyValuePair<int, string> (3,"Command");
由于键值对有解构函数Deconstruct(record记录也有解构函数)所以可以用作位模式匹配参数
支持switch表达式模式匹配 中的 属性模式
static void Main(string[] args)
{ var kvp =new KeyValuePair<int, string> (3,"Command");
Console.WriteLine(keyPattrn(kvp)); }
public static string keyPattrn(KeyValuePair<int, string> kvp) => kvp switch
{
(3, "Command") => "Origin",
(4, "Command") => "Origin",
_ => "Just a point",
};
支持is表达式 中的 属性模式
if(kvp is { Key: >2, Value: "Command" })
{
Console.WriteLine("是的");
}
if (kvp is (3, "Command"))
{
Console.WriteLine("是的");
}
//输出结果:
// 是的
// 是的
C# KeyValuePair<TKey,TValue>的用法的更多相关文章
- C# KeyValuePair<TKey,TValue>的用法-转载
C# KeyValuePair<TKey,TValue>的用法.结构体,定义可设置或检索的键/值对.也就是说我们可以通过 它记录一个键/值对这样的值.比如我们想定义一个ID(int类型)和 ...
- C# KeyValuePair<TKey,TValue>与Container
KeyValuePair<TKey,TValue> KeyValuePair<TKey,TValue>是一个结构体,相当于C#一个Map类型的对象,可以通过它记录一个键/值对 ...
- List<KeyValuePair<TKey,TValue>> 与 Dictionary<TKey,TValue> 不同
两者都可以通过 KeyValuePair<TKey,TValue> 进行遍历,并且两者可以相互转换: List<KeyValuePair<string,string>&g ...
- C# KeyValuePair<TKey,TValue> 与 Dictionary<TKey,TValue> 区别
KeyValuePair<TKey,TValue> 可以设置.查询的一对键值 是struct Dictionary<TKey,TValue> 可以设置.查询的多对键值的集合 总 ...
- .net源码分析 – Dictionary<TKey, TValue>
接上篇:.net源码分析 – List<T> Dictionary<TKey, TValue>源码地址:https://github.com/dotnet/corefx/blo ...
- IDictionary<TKey, TValue> vs. IDictionary
Enumerating directly over an IDictionary<TKey,TValue>returns a sequence of KeyValuePair struc ...
- Dictionary<TKey, TValue> 类
C# Dictionary<TKey, TValue> 类 Dictionary<TKey, TValue> 泛型类提供了从一组键到一组值的映射.字典中的每个添加项都由一个值及 ...
- wp7 BaseDictionary<TKey, TValue>
/// <summary>/// Represents a dictionary mapping keys to values./// </summary>/// /// &l ...
- C# .Net 中字典Dictionary<TKey,TValue>泛型类 学习浅谈
一.综述: Dictionary<TKey,TValue>是在 .NET Framework 2.0 版中是新增的.表示键值对的集合,Dictionary<TKey,TValue&g ...
随机推荐
- MySQL之MVCC与幻读
转自 https://blog.csdn.net/qq_31930499/article/details/110393988 如果是快照度,直接采用MVCC,如果是当前读,才会走next-key lo ...
- Windows和Linux关闭占用端口
关闭端口的方式有很多种,但是常用的就是这种比较来的快一点 如果通过以下方式解决不了,可以通过关闭服务来解决 Windows 1.查看端口占用的进程 netstat -ano | findstr 800 ...
- py笔记第一篇
#!/usr/bin/python #coding=utf-8 #@rename file #@2019/11/27 import os ls = os.rename('/root/tigergao. ...
- Java线上问题排查神器Arthas实战分析
概述 背景 是不是在实际开发工作当中经常碰到自己写的代码在开发.测试环境行云流水稳得一笔,可一到线上就经常不是缺这个就是少那个反正就是一顿报错抽风似的,线上调试代码又很麻烦,让人头疼得抓狂:而且deb ...
- 体验 正式发布 的OSM v1.0.0 版本
2021年10月份发布了OSM 1.0 RC[1],在过去的几个月里,OSM 的贡献者一直在努力为 v1.0.0 版本的发布做准备.2022年2月1日,OSM 团队正式发布 1.0.0 版本[2]. ...
- 【转】使用Docx.Core创建word表格
原文地址:https://www.cnblogs.com/qs315/p/13533765.html 使用Docx.Core创建word表格 下载DocxCore Nuget包 当前版本 1.0.7 ...
- php 递归目录
转载请注明来源:https://www.cnblogs.com/hookjc/ $TheFilePath='';function file_list($path){ global $TheFilePa ...
- undefined index: php中提示Undefined ...
我们经常接收表单POST过来的数据时报Undefined index错误,如下:$act=$_POST['action'];用以上代码总是提示Notice: Undefined index: act ...
- 测试人员学Java入门指南
目标读者 本指南特别适合有Python基础的同学学习Java入门,对于没有任何编程经验的同学可能会存在困难. 为什么测试人员要学Java 选择Java,应该不错.TesterHome测试论坛知名大佬基 ...
- 帆软报表(finereport)使用Event 事件对象 (target)修改提示框样式
target 事件属性 Event 对象 定义和用法 target 事件属性可返回事件的目标节点(触发该事件的节点),如生成事件的元素.文档或窗口. 语法 event.target 定义结束事件Jav ...