我正在尝试编写一个用数组内容填充字符串的函数,或者将它们设置为null.字符串的数量可以变化,我不想添加像它们一样的要求都是同一个数组或类的一部分.
在C#中你无法组合param和out.因此,这样做的唯一方法似乎是重载这样的方法:
public void ParseRemainders(string[] remainders, out string p1)
{
p1 = null;
if ((remainders != null) && (remainders.Length > 0))
p1 = remainders[0];
}
public void ParseRemainders(string[] remainders, out string p1, out string p2)
{
p1 = null;
p2 = null;
if (remainders != null)
{
ParseRemainders(remainders, out p1);
if (remainders.Length > 1)
p2 = remainders[1];
}
}
public void ParseRemainders(string[] remainders, out string p1, out string p2, out string p3)
{
p1 = null;
p2 = null;
p3 = null;
if (remainders != null)
{
ParseRemainders(remainders, out p1, out p2);
if (remainders.Length > 2)
p3 = remainders[2];
}
}
.... and on forever ....
Run Code Online (Sandbox Code Playgroud)
如何避免所有这些代码重复,理想情况下接受任意数量的参数?
编辑:这很有用,因为您可以这样做,ParseRemainders(remainders, out inputFileName, out outputFileName, out configFileName)然后避免必须手动执行
if (remainder.Length > 0) inputFileName = remainder[0];
if (remainder.Length > 1) outputFileName = remainder[1];
if (remainder.Length > 2) configFileName = remainder[2];
...
Run Code Online (Sandbox Code Playgroud)
对不起,如果不清楚,我有一个特定的目标,我为什么我不是简单地返回一个List<>.
结论:感谢BotondBalázs的答案,特别是暗示这被称为"阵列解构".正如他们所指出的那样,并且正如这个问题所证实的那样,在C#的当前版本中是不可能的:解构赋值 - C#中变量的对象属性
Eri*_*ert 12
到目前为止,我会采取与任何答案不同的方法.
static class Extensions {
public static SafeArrayReader<T> MakeSafe<T>(this T[] items)
{
return new SafeArrayReader<T>(items);
}
}
struct SafeArrayReader<T>
{
private T[] items;
public SafeArrayReader(T[] items) { this.items = items; }
public T this[int index]
{
get
{
if (items == null || index < 0 || index >= items.Length)
return default(T);
return items[index];
}
}
}
Run Code Online (Sandbox Code Playgroud)
在那里,现在你有一个数组,它给你一个默认值而不是抛出:
var remainder = GetRemainders().MakeSafe();
var input = remainder[0];
var output = remainder[1];
var config = remainder[2];
Run Code Online (Sandbox Code Playgroud)
十分简单.您对数据类型的语义有问题吗?创建一个更好的数据类型,封装所需的语义.
Bot*_*ázs 10
如果我理解正确,您的用例将如下所示:
var remainders = new[] { "a", "b", "c" };
string a, b, c;
ParseRemainders(remainders, a, b, c); // after this, a == "a", b == "b" and c == "c"
Run Code Online (Sandbox Code Playgroud)
您希望在C#中使用的功能称为数组解构,就像在JavaScript中一样:
var remainders = ["a", "b", "c"];
var [a, b, c] = remainders; // after this, a == "a", b == "b" and c == "c"
Run Code Online (Sandbox Code Playgroud)
不幸的是,据我所知,
这不能用C#以一般方式解决.
C#7虽然会有元组解构.
好吧,你可以改变你的方法
public IEnumerable<string> ParseRemainders(string[] remainders)
{
var result = new List<string>();
///... your logic here, fill list with your strings according to your needs
return result;
}
Run Code Online (Sandbox Code Playgroud)
Andys方法很好,但我会返回一个string[]因为它应该与输入数组具有相同的大小,并且null如果输入数组是null:
public string[] ParseRemainders(string[] remainders)
{
if(remainders == null) return null;
var parsed = new string[remainders.Length];
for(int i = 0; i < remainders.Length; i++)
parsed[i] = ParseRemainder(remainders[i]);
return parsed;
}
Run Code Online (Sandbox Code Playgroud)
澄清一下ParseRemainder(单个方法的不同方法string):
public string ParseRemainder(string remainder)
{
// parsing logic here...
return "the parsing result of remainder";
}
Run Code Online (Sandbox Code Playgroud)