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 ...
随机推荐
- 使用EdgyGeo Cesium工具查询下载数据集
Cesium中文网:http://cesiumcn.org/ | 国内快速访问:http://cesium.coinidea.com/ EdgyGeo, Inc. 带来了一件非常棒的工作支撑架构.工程 ...
- golang中使用switch语句根据年月计算天数
package main import "fmt" func main() { days := CalcDaysFromYearMonth(2021, 9) fmt.Println ...
- Python与Javascript相互调用超详细讲解(四)使用PyNode进行Python与Node.js相互调用项(cai)目(keng)实(jing)践(yan)
目录 前提 安装 使用 const pynode = require('@fridgerator/pynode')的时候动态链接错误 ImportError: math.cpython-39-x86_ ...
- Java 锁 概念介绍
一 Java中的锁是什么? /* * 一 Java锁定义? * 在计算机科学中,锁(lock)或互斥(mutex)是一种同步机制,用于在有许多执行线程的环境中强制对资源的访问限制. * 锁旨在强制 ...
- JDBC 操作预编译语句中LIKE模糊匹配怎么用
问题描述 在使用JDBC 预编译执行语句时,遇到一个问题,那就是在含有LIKE的查询语句时,我到底怎么使用匹配符%._呢. 如: SELECT * FROM "+LQ_USERS+" ...
- Java.lang.Math类详解
Math类位于Java.lang包中,包含用于执行基本数学运算的方法!Math类的所有执行方法都是静态方法,可以直接使用类名.方法名调用,如:Math.round() 常用的方法:Math.round ...
- select 级联选择
转载请注明来源:https://www.cnblogs.com/hookjc/ <script language="javascript"> <!-- ...
- js _proto_和prototype 区别 剖析
首先,要明确几个点: 1.在JS里,万物皆对象.方法(Function)是对象,方法的原型(Function.prototype)是对象.因此,它们都会具有对象共有的特点. 即:对象具有属性__pro ...
- 关于CSS3样式中的前缀问题
作为新手,有的时候在写css时分不清什么属性需要用到前缀,或者用什么前缀,下面是我平时学习的一些总结. 在了解这些前缀之前,先介绍一下各大主流浏览器的内核: IE--trident(国内很多双核浏览器 ...
- oracle查看当前正在使用的数据库
select name from V$DATABASE; 也可以用 select SYS_CONTEXT('USERENV','INSTANCE_NAME') from dual;