Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何根据参数的数量更改参数传递给string.format()函数的方式?_C#_Unity3d - Fatal编程技术网

C# 如何根据参数的数量更改参数传递给string.format()函数的方式?

C# 如何根据参数的数量更改参数传递给string.format()函数的方式?,c#,unity3d,C#,Unity3d,目前,我正在尝试编写一些东西,从txt文件中提取字符串并将其输入到数组中。我是通过手动输入它们并使用插值字符串来实现的,但现在这已经不可行了。我需要能够根据函数的结果更改字符串的各个部分,并且在任何给定的字符串上,可以有0到任意数量的需要更改的部分。我认为这在理论上是可行的,但必须有更好的方法: public void formatStringInSentencesArray(int numOfArgs, int arrIndexToBeFormatted, UnityAction[] fun

目前,我正在尝试编写一些东西,从txt文件中提取字符串并将其输入到数组中。我是通过手动输入它们并使用插值字符串来实现的,但现在这已经不可行了。我需要能够根据函数的结果更改字符串的各个部分,并且在任何给定的字符串上,可以有0到任意数量的需要更改的部分。我认为这在理论上是可行的,但必须有更好的方法:

 public void formatStringInSentencesArray(int numOfArgs, int arrIndexToBeFormatted, UnityAction[] funcsToBePutIn)
    {
        if (numOfArgs == 1)
        {
            conversation[index].sentences[arrIndexToBeFormatted] = string.Format(conversation[index].sentences[arrIndexToBeFormatted], funcsToBePutIn[0]);
        }
    ...
        else if (numOfArgs == 5)
        {
            conversation[index].sentences[arrIndexToBeFormatted] = string.Format(conversation[index].sentences[arrIndexToBeFormatted], funcsToBePutIn[0], funcsToBePutIn[1], funcsToBePutIn[2], funcsToBePutIn[3], funcsToBePutIn[4]);
        }
有没有什么方法可以让我做这件事,而不仅仅是把一堆if和else if拼凑在一起?(这都是用C写的,对于unity游戏顺便说一句)

欢迎来到SO

string.format已支持数组。这就是你要找的吗

var paramArray = new string[] { "a", "b", "c", "d", "e" };
var output = string.Format("{0} {1} {2} {3}", paramArray);
根据您的示例,我可能会将您的函数替换为:

conversation[index].sentences[arrIndexToBeFormatted] = string.Format(conversation[index].sentences[arrIndexToBeFormatted], funcsToBePutIn);
欢迎来到SO

string.format已支持数组。这就是你要找的吗

var paramArray = new string[] { "a", "b", "c", "d", "e" };
var output = string.Format("{0} {1} {2} {3}", paramArray);
根据您的示例,我可能会将您的函数替换为:

conversation[index].sentences[arrIndexToBeFormatted] = string.Format(conversation[index].sentences[arrIndexToBeFormatted], funcsToBePutIn);
通过使用params关键字,可以指定一个方法参数,该参数接受可变数量的参数。参数类型必须是一维数组

在方法声明中,params关键字之后不允许有其他参数,并且在方法声明中只允许有一个params关键字

编译器将处理将该行参数转换为单个数组的问题

String.Format()
-和像
Console.WriteLine()
这样使用它的函数使用params关键字。main函数的行为与它的行为类似,但如果数据来自操作系统,则可能没有使用关键字(生成数组是其他人的工作)

通过使用params关键字,可以指定一个方法参数,该参数接受可变数量的参数。参数类型必须是一维数组

在方法声明中,params关键字之后不允许有其他参数,并且在方法声明中只允许有一个params关键字

编译器将处理将该行参数转换为单个数组的问题


String.Format()
-和像
Console.WriteLine()
这样使用它的函数使用params关键字。main函数的行为与它的行为类似,但如果数据来自操作系统,则可能没有使用关键字(使数组成为其他人的工作)。

else。。。如果构造通常指示您应该在该位置使用开关/案例。不久前,他们获得了模式匹配能力params机修工可能就是你要的机器人:否则。。。如果构造通常指示您应该在该位置使用开关/案例。不久前,他们获得了模式匹配能力params机修工可能就是你要的机器人:我不知道,是的,那就是我需要的,谢谢。我不知道,是的,那就是我需要的,谢谢