简体   繁体   English

C#为可变数量的参数构建参数列表的简单方法

[英]C# simple way to build params list for variable number of params

I have a method that takes this parameter 我有一个采用此参数的方法

params string[] additionalParameters 

I am building it like this: 我正在构建它像这样:

qsParams = new string[] {"k=" + k, "l=" + l, "o=" + o, "s=" + s, "t=" + t, "h=" + h, "exp=" + exp };

These are url params. 这些是url params。 The problem is I only want to add parameters where the variables are not null or empty. 问题是我只想添加变量不为null或为空的参数。

I can do this kind of thing: 我可以做这样的事情:

if (string.IsNullOrEmpty(k))
{
    qsParams = new string[] {"l=" + l, "o=" + o, "s=" + s, "t=" + t, "h=" + h, "exp=" + exp };
}

But that's going to get complicated and ugly trying to handle all the different permutations of empty variables. 但是,这将变得复杂和丑陋,试图处理空变量的所有不同排列。

Can anyone suggest a simple way to add the params if there are not null? 如果没有null,有人可以建议一个简单的方法来添加参数吗? Perhaps a list that I can convert to a params []string? 也许我可以转换为params []字符串的列表?

    public static void Main(string[] args)
    {

        List<string> parameters = new List<string>();

        string k = "a";
        string l = null;

        AddParam("k", k, parameters);
        AddParam("l", l, parameters);


        string[] result = parameters.ToArray();
    }

    public static void AddParam(string paramName, string paramValue, List<string> parameters)
    {
        if (string.IsNullOrEmpty(paramValue))
            return;

        parameters.Add(paramName + "=" + paramValue);
    }

You can try something like this. 你可以尝试这样的事情。

You can write a method that returns null if your variable has no value: 如果变量没有值,则可以编写一个返回null的方法:

private string GetListValue(string prefix, string value) {
    if (String.IsNullOrEmpty(value)) {
        return null;
    }
    else {
        return prefix + value;
    }
}

You can define your raw list with this method (only using 2 values): 您可以使用此方法定义原始列表(仅使用2个值):

string[] rawList = { GetListValue("k=", k), GetListValue("l=", l) };

Then clean the list with LINQ: 然后用LINQ清理列表:

string[] cleanValues = rawValues.Where(v => v != null).ToArray();

If your params are always in right order: 如果您的参数始终处于正确的顺序:

List<string> qsParams = new List<string>();
string[] paramNames = new string[] { "k", "l", "o", "s", "t", "h", "exp" };
for (int i = 0; i < additionalParameters.Length; i++)
{
    if (!string.IsNullOrEmpty(additionalParameters[i]))
    {
        qsParams.Add(string.Format("{0}={1}", paramNames[i], additionalParameters[i]));
    }
}

Just create a dictionary with the key being the param and the value being well... the value. 只需创建一个字典,其中键是param,值很好......值。

Dictionary<string, string> Parameters = new Dictionary<string, string>();

Parameters.Add("k", "myKValue");
Parameters.Add("o", "myOValue");

string paramaterList = string.Join("&", Parameters.Select(x => $"{x.Key}={x.Value}"));

Only add values to the dictionary when they aren't null. 仅当字典不为空时才将值添加到字典中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM