Aks*_*tha 1 c# arrays string.format
我有一个阵列
ArrayList array = new ArrayList();
array.Add("a");
array.Add("b");
array.Add("c");
Run Code Online (Sandbox Code Playgroud)
我有一个字符串变量refFormat,其格式如下.
string refFormat = "{2} {0}";
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用此格式从数组中获取一串值.以下是我写的内容.
string newStr = String.Format(refFormat,array.ToArray());
Run Code Online (Sandbox Code Playgroud)
当我尝试这样做时,我得到以下异常.
索引(从零开始)必须大于或等于零且小于参数列表的大小.
我知道这个问题听起来很重复,但我怀疑的是如何从数组中选择其索引是以2和0格式指定的值.请帮忙..
编辑:对不起提出错误的问题.我正在使用arraylist而不是字符串数组我正在尝试相同的.尽管使用ToArray()将其转换为数组,我仍然得到异常.我哪里错了?而且我也不能在这里使用List而不是arraylist,因为数组包含不同类型的数据.请帮帮我..
作为参考,这里也是我的工作代码:
string[] array = new string[] { "a", "b", "c", "d" };
string refFormat = "{2} {0}";
string newStr = String.Format(refFormat, array);
Console.WriteLine(newStr);
Run Code Online (Sandbox Code Playgroud)
运行上面的代码时没有遇到错误.