在 C# 7 中是否可以将元组解构为方法参数 [英] In C# 7 is it possible to deconstruct tuples as method arguments
本文介绍了在 C# 7 中是否可以将元组解构为方法参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如我有
private void test(Action<ValueTuple<string, int>> fn)
{
fn(("hello", 10));
}
test(t =>
{
var (s, i) = t;
Console.WriteLine(s);
Console.WriteLine(i);
});
我想写这样的东西
private void test(Action<ValueTuple<string, int>> fn)
{
fn(("hello", 10));
}
test((s,i) =>
{
Console.WriteLine(s);
Console.WriteLine(i);
});
用一些适当的符号可以做到这一点吗?
Is this possible with some proper notation?
推荐答案
您可以将其缩短为:
void test( Action<ValueTuple<string, int>> fn)
{
fn(("hello", 10));
}
test(((string s, int i) t) =>
{
Console.WriteLine(t.s);
Console.WriteLine(t.i);
});
希望有一天我们可以将元组中的参数传递给方法调用:
Hopefully, one day we might be able to splat the parameters from a tuple to the method invocation:
void test(Action<ValueTuple<string, int>> fn)
{
fn(@("hello", 10)); // <-- made up syntax
}
test((s, i) =>
{
Console.WriteLine(s);
Console.WriteLine(i);
});
但目前没有.
这篇关于在 C# 7 中是否可以将元组解构为方法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文