是否可以解构出 ValueTuple 参数?
Is it possible to deconstruct out ValueTuple parameters?
是否可以解构不是从方法返回但作为输出参数的元组?我不确定我是否正确表达了自己,甚至使用了正确的术语,所以这里有一些例子:
void OutMethod(out (int aNumber, string someText) output)
=> output = (15, "yo");
void Usage()
{
{
// Works, of course.
OutMethod(out var tuple);
// But *slightly* aesthetically unappealing to use.
var usage = $"{tuple.someText}: {tuple.aNumber}";
}
{
// Would be awesome, but doesn't work.
OutFunction(out var(number, text));
}
{
// Would be awesome too, but doesn't work.
OutFunction(out (var number, var text));
}
{
// This doesn't work either.
OutFunction((out var number, out var text));
}
{
// Not even this.
OutFunction((out int number, out string text));
}
{
// Or this.
OutMethod(out (int number, string text));
}
{
// Or this.
int number;
string text;
OutMethod(out (number, text));
}
}
顺便说一句,不是抱怨。只是想知道我是否遗漏了什么。
这目前是不可能的。 Further, according to this comment on the CSharplang github repo:
This was something that we took up in the LDM [Language Design Meetings] when we were triaging and decided against doing, at least for the near future...
因此它可能会在未来一段时间内保持 "isn't possible"。
虽然另一个答案是正确的,因为它不能作为函数调用的一部分在一行中完成,但您当然可以根据需要手动解构 out 参数:
OutMethod(out var tuple);
(int aNumber, string someText) = tuple;
var usage = $"{someText}: {aNumber}";
这至少会解决您的使用问题。
是否可以解构不是从方法返回但作为输出参数的元组?我不确定我是否正确表达了自己,甚至使用了正确的术语,所以这里有一些例子:
void OutMethod(out (int aNumber, string someText) output)
=> output = (15, "yo");
void Usage()
{
{
// Works, of course.
OutMethod(out var tuple);
// But *slightly* aesthetically unappealing to use.
var usage = $"{tuple.someText}: {tuple.aNumber}";
}
{
// Would be awesome, but doesn't work.
OutFunction(out var(number, text));
}
{
// Would be awesome too, but doesn't work.
OutFunction(out (var number, var text));
}
{
// This doesn't work either.
OutFunction((out var number, out var text));
}
{
// Not even this.
OutFunction((out int number, out string text));
}
{
// Or this.
OutMethod(out (int number, string text));
}
{
// Or this.
int number;
string text;
OutMethod(out (number, text));
}
}
顺便说一句,不是抱怨。只是想知道我是否遗漏了什么。
这目前是不可能的。 Further, according to this comment on the CSharplang github repo:
This was something that we took up in the LDM [Language Design Meetings] when we were triaging and decided against doing, at least for the near future...
因此它可能会在未来一段时间内保持 "isn't possible"。
虽然另一个答案是正确的,因为它不能作为函数调用的一部分在一行中完成,但您当然可以根据需要手动解构 out 参数:
OutMethod(out var tuple);
(int aNumber, string someText) = tuple;
var usage = $"{someText}: {aNumber}";
这至少会解决您的使用问题。