c# - 如何在 C# 中构建具有未指定数量参数的方法

标签 c# parameters

这是我的代码:

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

但是我需要类似的东西

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

如果我没记错的话,string.format 有点像这样......

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

C# 中有什么东西可以让我这样做吗?

我知道我能做到

private static string AddURISlash(string[] remotePath)

但这不是我们的想法。

如果这在某些框架中可以完成,而在其他框架中则不能,请具体说明以及如何解决。

提前致谢

最佳答案

我想你想要一个 parameter array :

private static string CreateUriFromSegments(params string[] segments)

然后您实现它知道 remotePath 只是一个数组,但是您可以调用它:

string x = CreateUriFromSegments("http://foo.bar", "x", "y/", "z");

(如注释中所述,参数数组只能作为声明中的最后一个参数出现。)

关于c# - 如何在 C# 中构建具有未指定数量参数的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9487724/

相关文章:

c# - 当尝试写入文件抛出 UnauthorizedAccessException 时,需求不会抛出异常

c# - 在 asp.net 中处理危险字符的最佳做法是什么?

c# - 使用变量作为循环的终点 - C#

javascript - 如何在ajax post上传递对象数组作为参数

c++ - 如何将矩阵参数发送到 C++ 中的 main 函数

MySQL存储过程在参数中传递数字数组

C# 引用参数传递

c# - 读取大型 xml 文件的最快方法

java - 参数未知(Play! Framework)

c# - Foreach循环检查空字符串并为其赋值c#