DELPHI中对应C#中的key-value的类型:KeyValuePair

C#中的定义:

Dictionary<int, string> sl = new Dictionary<int, string>();

foreach (KeyValuePair<int, string> kvp in sl)

Delphi中可以用:

var sl :TStringList;

sl := TStringList.Create;

sl.Values['1'] := '123';
sl.AddPair('300','DC');

DELPHI的ValueListEditor控件可能用起来更简单;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# ,可以通过 Dictionary<TKey, TValue> 类型的 ContainsValue() 和 FirstOrDefault() 方法实现查找字典某个值对应的键。示例如下: ```csharp Dictionary<string, int> myDict = new Dictionary<string, int>() { { "apple", 1 }, { "banana", 2 }, { "orange", 3 }, }; int targetValue = 2; if (myDict.ContainsValue(targetValue)) { var keyValuePair = myDict.FirstOrDefault(x => x.Value == targetValue); string targetKey = keyValuePair.Key; Console.WriteLine($"The key for value {targetValue} is {targetKey}"); } else { Console.WriteLine($"The value {targetValue} is not found in the dictionary"); } ``` 在上述示例,我们先定义了一个 Dictionary<string, int> 类型的 myDict 变量,并初始化了一些键值对。然后,我们定义了一个整型变量 targetValue,它表示要查找的值。接着,我们使用 ContainsValue() 方法判断字典是否包含该值。如果包含,我们使用 FirstOrDefault() 方法找到第一个值等于 targetValue 的键值对,然后通过该键值对的 Key 属性获取对应的键名,最后输出结果。如果该值不存在于字典,我们直接输出提示信息。 需要注意的是,在使用 FirstOrDefault() 方法时,我们需要传入一个 Lambda 表达式作为参数,用于定义筛选条件。该 Lambda 表达式的 x 表示一个键值对,我们通过 x.Value == targetValue 来判断该键值对的值是否等于 targetValue。如果条件成立,该键值对就符合要求,FirstOrDefualt() 方法就会返回它。如果条件不成立,方法会返回默认值 null,表示未找到符合条件的键值对。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值