首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在C#中声明KeyValuePair的元组

在C#中声明KeyValuePair的元组
EN

Stack Overflow用户
提问于 2019-03-01 07:30:38
回答 2查看 357关注 0票数 1

为了我的代码,我需要一个包含两个组件的元组,这两个组件都是KeyValuePairs。然而,在我的生命中,我甚至不知道如何声明这个东西。我让它在普通字符串上工作。

代码语言:javascript
复制
Tuple<string, string> t = new Tuple<string, string>("abc", "123");

但是我需要KeyValue对而不是字符串,我尝试过这样的方法,但是它拒绝编译,因为构造函数不能接受2个参数。

代码语言:javascript
复制
Tuple<KeyValuePair<string, string>, KeyValuePair<string,string>> a = 
    new Tuple<KeyValuePair<string, string> ("a", "1"), 
    KeyValuePair<string, string> ("b", "2");

任何指导都将不胜感激。如果对您有帮助,请随时使用:https://dotnetfiddle.net/y2rTlM

EN

回答 2

Stack Overflow用户

发布于 2019-03-01 07:32:59

使用:

代码语言:javascript
复制
Tuple<KeyValuePair<string, string>, KeyValuePair<string, string>> a =
        new Tuple<KeyValuePair<string, string>, KeyValuePair<string, string>>(
            new KeyValuePair<string, string>("a", "1"),
            new KeyValuePair<string, string>("b", "2")
        );
票数 2
EN

Stack Overflow用户

发布于 2019-03-01 08:26:12

或者,简而言之:

代码语言:javascript
复制
using KVPS = System.Collections.Generic.KeyValuePair<string, string>;

namespace Test 
{
    class Program
    {
        static void Main(string[] args)
        {
            Tuple<KVPS, KVPS> a =
                Tuple.Create(
                    new KVPS("a", "1"),
                    new KVPS("b", "2")
                    );
            Console.WriteLine($"{a.Item1.Key} {a.Item1.Value} : {a.Item2.Key} {a.Item2.Value}");
        }
    }
}   

如果你有很多元组和类似的元组,这可能会很有用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54935871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档