C#:将集合转换为 params[]

发布于 2024-10-30 16:24:22 字数 268 浏览 2 评论 0原文

以下是我的代码的简化:

void Foo(params object[] args)
{
    Bar(string.Format("Some {0} text {1} here {2}", /* I want to send args */);
}

string.Format 要求将参数作为 params 发送。有什么方法可以将 args 集合转换为 string.Format 方法的参数吗?

Here is a simplification of my code:

void Foo(params object[] args)
{
    Bar(string.Format("Some {0} text {1} here {2}", /* I want to send args */);
}

string.Format requires the arguments sent as params. Is there some way I can convert the args collection into parameters for the string.Format method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

面犯桃花 2024-11-06 16:24:22

params 关键字只是语法糖,它允许您使用任意数量的参数调用此类方法。但是,这些参数始终作为数组传递给该方法。

这意味着 Foo(123, "hello", DateTime.Now) 相当于 Foo(new object[] { 123, "hello", DateTime.Now })

因此,您可以将参数从 Foo 直接传递到 string.Format,如下所示:

void Foo(params object[] args)
{
  Bar(string.Format("Some {0} text {1} here {2}", args));
}

但是,在这种特殊情况下,您需要三个参数(因为您有 {0}、{ 1} 和 {2}(按照您的格式)。因此,您应该将代码更改为:

void Foo(object arg0, object arg1, object arg2)
{
  Bar(string.Format("Some {0} text {1} here {2}", arg0, arg1, arg2));
}

...或按照马塞洛的建议进行操作。

The params keyword is only syntactic sugar that allows you to call such a method with any number of arguments. However, those arguments are always passed to the method as an array.

This means that Foo(123, "hello", DateTime.Now) is equivalent to Foo(new object[] { 123, "hello", DateTime.Now }).

You can therefore pass the arguments from Foo directly to string.Format like this:

void Foo(params object[] args)
{
  Bar(string.Format("Some {0} text {1} here {2}", args));
}

However, in this particular case, you demand three arguments (because you have {0}, {1} and {2} in your format). Therefore you should change your code to:

void Foo(object arg0, object arg1, object arg2)
{
  Bar(string.Format("Some {0} text {1} here {2}", arg0, arg1, arg2));
}

...or do as Marcelo suggested.

白鸥掠海 2024-11-06 16:24:22

将它们作为单个参数传递:

Bar(string.Format("Some {0} text {1} here {2}", args));

Pass them in as a single argument:

Bar(string.Format("Some {0} text {1} here {2}", args));
腹黑女流氓 2024-11-06 16:24:22

例如,您可以尝试使用 object.GetType()

void Foo(params object[] args)
    {
        List<string> argStrings = new List<string>();

        foreach (object arg in args)
        {
            if (args.GetType().Name == typeof(String).Name)
            {
                argStrings.Add((string)arg);
            }
            else if (args.GetType().Name == typeof(DateTime).Name)
            {
                DateTime dateArg = (DateTime)arg;
                argStrings.Add(dateArg.ToShortDateString());
            }
            else
            {
                argStrings.Add(arg.ToString());
            }
        }

        Bar(string.Format("Some {0} text {1} here {2}", argStrings.ToArray()));
    }

You could try using object.GetType(), for example

void Foo(params object[] args)
    {
        List<string> argStrings = new List<string>();

        foreach (object arg in args)
        {
            if (args.GetType().Name == typeof(String).Name)
            {
                argStrings.Add((string)arg);
            }
            else if (args.GetType().Name == typeof(DateTime).Name)
            {
                DateTime dateArg = (DateTime)arg;
                argStrings.Add(dateArg.ToShortDateString());
            }
            else
            {
                argStrings.Add(arg.ToString());
            }
        }

        Bar(string.Format("Some {0} text {1} here {2}", argStrings.ToArray()));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文