如何在C#中使用未指定数量的参数构建方法 [英] How to build a method with unspecified amount of params en C#

查看:122
本文介绍了如何在C#中使用未指定数量的参数构建方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

    private static string AddURISlash(string remotePath)
    {
        if (remotePath.LastIndexOf("/") != remotePath.Length - 1)
        {
            remotePath += "/";
        }
        return remotePath;
    }

但是我需要类似的东西

AddURISlash("http://foo", "bar", "baz/", "qux", "etc/");

如果我没记错的话,string.format就是这样...

If I recall correctly, string.format is somehow like that...

String.Format("{0}.{1}.{2}.{3} at {4}", 255, 255, 255, 0, "4 p.m.");

C#中是否有可以允许我这样做的东西?

Is there something in C# that allows me to do so?

我知道我能做到

private static string AddURISlash(string[] remotePath)

但这不是主意.

如果这可以在某些框架中完成,而在其他框架中则不可以,请指定其解决方法.

If this is something in some framework can be done and in others not please specify and how to resolve it.

预先感谢

推荐答案

您可以使用params,它可以让您指定任意数量的参数

You can use params, which lets you specify any amount of arguments

private static string AddURISlash(params string[] remotePaths)
{
    foreach (string path in remotePaths)
    {
        //do something with path
    }
}

请注意,params将影响代码的性能,因此请谨慎使用.

Note that params will impact the performance of your code, so use it sparingly.

这篇关于如何在C#中使用未指定数量的参数构建方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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