简洁地创建IDictionary< _,obj> [英] Concisely creating an IDictionary<_,obj>

查看:52
本文介绍了简洁地创建IDictionary< _,obj>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种较短的方法来创建 IDictionary< _,obj> ,可能不需要装箱每个值?这就是我所拥有的.

Is there a shorter way of creating an IDictionary<_,obj>, possibly without boxing every value? This is what I have.

let values =
  [ "a", box 1
    "b", box "foo"
    "c", box true ]
  |> dict

Dictionary< _,obj> .Add 可以不用装箱而调用,但是我想不出一种比我更短的使用方法.

Dictionary<_,obj>.Add can be called without boxing, but I couldn't figure out a way to use it that's shorter than what I have.

除了定义拳击员之外,我还希望有其他东西.

I'm hoping for something other than defining a boxing operator.

根据布赖恩(Brian)的建议,这是一种实现方法,但是它有其自身的问题.

Based on Brian's suggestion, here's one way to do it, but it has its own problems.

let values =
  Seq.zip ["a"; "b"; "c"] ([1; "foo"; true] : obj list) |> dict

推荐答案

以下是kvb的建议(到目前为止,可能是最简洁,最清晰的):

Here's a solution, following kvb's suggestion (probably the most concise, and clearest, so far):

let inline (=>) a b = a, box b

let values =
  [ "a" => 1
    "b" => "foo"
    "c" => true ]
  |> dict

这篇关于简洁地创建IDictionary&lt; _,obj&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆