c#中的JavaScript扩展语法
c#中是否有任何实现,例如 >?
var arr = new []{"Hello", "World"};
Console.WriteLine(...arr);
第三方
使用方法编辑
public void greet(string salutation, string recipient)
{
Console.WriteLine(salutation + " " + recipient);
}
// instead of this
greet(arr[0], arr[1]);
// the spread syntax in javascript allows this
greet(...arr);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
则可以执行以下
打印4
如果要实现列表或列表的初始化,
you can also do the following
will print 4
if you want to achieve initialization of lists or arrays with both an accompanying enumerable and parameters
C#12添加了传播功能。
https://learn.microsoft.com/en-us /dotnet/csharp/wheck-new/csharp-12
旧答案(pre c#12)
没有传播选项,而是一些有用的选择。
但是,您可以通过各种语言功能获得类似的功能。
回答您的示例:
c#
您提供的链接有此示例:
javascript vread
参数
在C#中,具有相同类型
的C#,具有不同的数字类型,使用double
反射
在C#中,具有不同的数字类型,使用对象和反射,这可能是最接近您所要求的数字。
C# 12 has added the spread feature.
https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-12
Old answer (pre C# 12)
There isn't a spread option, but some useful alternatives.
However, having said that, you can get similar functionality with various language features.
Answering your example:
C#
The link you provide has this example:
Javascript Spread
Params
In C#, with same type
In C#, with different numeric types, using double
Reflection
In C#, with different numeric types, using object and reflection, this is probably the closest to what you are asking for.
C#12引入了类似于JavaScript的传播操作员。它可以使用.NET 8
我们可以写
C# 12 has introduced the spread operator similar to Javascript. It is available with .Net 8
We can write
获得与此类似的行为(无反思)的一个窍门是接受
params someObject [] [] []
,还要从somebobject
tosomeobject定义隐式运算符[]
。现在,您可以将someObject
和单个someObject
元素的数组的混合物传递。One trick to get a behavior similar to this (without reflection) is to accept
params SomeObject[][]
and to also define an implicit operator fromSomeObject
toSomeObject[]
. Now you can pass a mixture of arrays ofSomeObject
and individualSomeObject
elements.C#中没有直接的预构建库来处理内置在spread中的内容
,以便在C#中获得该功能,您需要通过其访问修饰符来反映对象并获取方法,属性或字段。
您会做类似的操作:
然后迭代并将它们扔进动态对象:
https://www.oreilly.com/learning/building-c-objects-dynancily
there is no direct pre-built library in C# to handle what is built into Spread
In order to get that functionality in C#, you need to Reflect the object and get the methods, properties, or fields by their access modifiers.
You'd do something like:
then iterate through and throw them into your dynamic object:
https://www.oreilly.com/learning/building-c-objects-dynamically
我来这里寻找 c#range运算符如
数字[1 .. 4
] ”显示问题标记为'linqpad'“ rel =” tag'
标记为“ linqpad”的问题“ aria-label = “ https://i.sstatic.net/wdjag.png” rel =“ nofollow noreferrer”>
I came here looking for the c# range operator as in
numbers[ 1 .. 4]
In linqpad this looks like this