C#-创建List <keyvaluepair >>作为方法参数的简便方法

时间:2018-11-23 18:45:39

标签: c#

我很确定有一个简单的解决方案可以解决我的问题。但是我找不到它

假设我有一个定义如下的方法:

public bool Insert(string table, List<KeyValuePair<string, string>> fields) { }

现在,我正在这样调用函数:

db.Insert("personen", new List<KeyValuePair<string, string>> { 
    new KeyValuePair<string, string>("firstname", "Dominic") });

有没有办法缩短这条无限长的线? 为了清楚起见,我的意思是电话的这一部分:

new List<KeyValuePair<string, string>> { 
    new KeyValuePair<string, string>("firstname", "Dominic") }

例如,在C ++中,您可以/将这样做:

db.Insert("personen", {{"firstname", "Dominic"}});

使用多个值会更好:

db.Insert("personen", {
    { "firstname", "Test" },
    { "lastname", "Test 2" },
    { "address", "Teest" }
});

C#版本:v4.0.30319 .Net版本:4.6.1

3 个答案:

答案 0 :(得分:4)

我认为静态工厂和参数的混合与您将要得到的尽可能接近:

static bool Insert(string table, params KeyValuePair<string, string>[] fields) {...}

class KeyValueFactory
{
    public static KeyValuePair<TKey, TValue> of<TKey, TValue>(TKey key, TValue value)
    {
        return new KeyValuePair<TKey, TValue>(key, value);
    }
}

用法示例:

        Insert(
           "personen",
            KeyValueFactory.of("firstname", "Dominic"),
            KeyValueFactory.of("lastName", "test2"));

答案 1 :(得分:1)

此实现证明您可以使用using <symbol>=<type>;子句来减少混乱,而不会对其余代码产生太大影响。您只需要带有// $$$注释的行,剩下的只是繁琐的工作。它将在v4.x上编译并运行:

using System.Collections.Generic;
using K = System.Collections.Generic.KeyValuePair<string, string>; // $$$

public class MainClass
{
    public static void Main(string[] args)
    {
        DBClass db;

        db = new DBClass();

        db.Insert("personen", new List<K> {  // $$$
            new K("firstname", "Dominic")    // $$$
        });
    }
}

public class DBClass
{
    public bool Insert(string name, List<KeyValuePair<string, string>> values)
    {
        // ...

        return false;
    }
}

最简单的版本如下,但是在db方面需要一些帮助。如您在Insert()调用中所见,这可以完全减少混乱:

using System.Collections.Generic;

public class MainClass
{
    public static void Main(string[] args)
    {
        DBClass db;

        db = new DBClass();

        db.Insert("personen",
            "firstname", "Dominic",
            "lastname", "Cinimod"
        );
    }
}

public class DBClass
{
    public bool Insert(string name, params string[] values)
    {
        if (values.Length % 2 == 1)
        {
            throw new System.ArgumentException("wrong number of arguments");
        }

        // ...
    }
}

我想对第二种解决方案更为清楚,因为它极富争议。奇偶校验之后,您应该将值打包在KVP中:

        if (values.Length % 2 == 1)
        {
            throw new System.ArgumentException("wrong number of arguments");
        }

        KVPs = new List<KeyValuePairs<string, string>>();
        for (i = 0; i < values.Length; i += 2)
        {
            KVPs.Add(new KeyValuePair<string, string>(values[i], values[i + 1]));
        }

完成此操作后,只需委托给原始方法即可:

        return this.Insert(name, KVPs); // pass on the returned bool!

由于两个重载都可以使用。非结构化版本的代码混乱程度较低,但无法进行静态检查。您需要运行代码以在运行时查看是否引发异常或运行平稳。因此,从C#的角度来看,它实际上是“更丑陋的”,强类型试图尽力利用静态依赖项,以便进行编译时检查。

答案 2 :(得分:0)

您可以改用字典。

IDictionary<int, string> dict = new Dictionary<int, string>()
                                            {
                                                {1,"One"},
                                                {2, "Two"},
                                                {3,"Three"}
                                            };
相关问题