C#中的JavaScript扩展语法

jmv*_*dad 27 javascript c# spread-syntax

在C#中是否有像JavaScript的扩展语法一样的实现?

var arr = new []{
   "1",
   "2"//...
};

Console.WriteLine(...arr);
Run Code Online (Sandbox Code Playgroud)

Rhy*_*ous 13

没有传播选项.而且有原因.

  1. 除非使用params关键字,否则属性不是C#中的数组
  2. 使用param关键字的属性必须:
    1. 分享相同的类型
    2. 有一个可转换的共享类型,例如double用于数字
    3. 属于object []类型(因为对象是所有东西的根类型)

但是,话虽如此,您可以获得具有各种语言功能的类似功能.

回答你的例子:

C#

var arr = new []{
   "1",
   "2"//...
};

Console.WriteLine(string.Join(", ", arr));
Run Code Online (Sandbox Code Playgroud)

您提供的链接有以下示例:

Javascript传播

function sum(x, y, z) {
  return x + y + z;
}

const numbers = [1, 2, 3];

console.log(sum(...numbers));
// expected output: 6

console.log(sum.apply(null, numbers));
Run Code Online (Sandbox Code Playgroud)

C#中的参数,具有相同的类型

public int Sum(params int[] values)
{
     return values.Sum(); // Using linq here shows part of why this doesn't make sense.
}

var numbers = new int[] {1,2,3};

Console.WriteLine(Sum(numbers));
Run Code Online (Sandbox Code Playgroud)

在C#中,使用不同的数字类型,使用double

public int Sum(params double[] values)
{
     return values.Sum(); // Using linq here shows part of why this doesn't make sense.
}

var numbers = new double[] {1.5, 2.0, 3.0}; // Double usually doesn't have precision issues with small whole numbers

Console.WriteLine(Sum(numbers));
Run Code Online (Sandbox Code Playgroud)

反射 在C#中,使用不同的数字类型,使用对象和反射,这可能是最接近你要求的.

using System;
using System.Reflection;

namespace ReflectionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var paramSet = new object[] { 1, 2.0, 3L };
            var mi = typeof(Program).GetMethod("Sum", BindingFlags.Public | BindingFlags.Static);
            Console.WriteLine(mi.Invoke(null, paramSet));
        }

        public static int Sum(int x, double y, long z)
        {
            return x + (int)y + (int)z;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 您的反射示例实际上更类似于 JavaScript 的 [`Function.prototype.apply()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/ apply),这是一个很棒的 ES6 之前的特性,在扩展语法可用之前被大量使用,但无论如何都是很好的建议(尽管我不会在性能关键的生产代码中推荐它)。 (2认同)

Sre*_*raj 9

C# 12 引入了类似于 Javascript 的扩展运算符。它适用于 .Net 8

我们可以写

int[] row0 = [1, 2, 3];
int[] row1 = [4, 5, 6];
int[] row2 = [7, 8, 9];
int[] single = [..row0, ..row1, ..row2];
Run Code Online (Sandbox Code Playgroud)


Ian*_*cer 7

获得与此类似的行为(无需反射)的一个技巧是接受params SomeObject[][]并定义一个隐式运算符 from SomeObjectto SomeObject[]SomeObject现在您可以传递数组和单个元素的混合SomeObject

public class Item
{
    public string Text { get; }

    public Item (string text)
    {
        this.Text = text;
    }

    public static implicit operator Item[] (Item one) => new[] { one };
}

public class Print
{
    // Accept a params of arrays of items (but also single items because of implicit cast)

    public static void WriteLine(params Item[][] items)
    {
        Console.WriteLine(string.Join(", ", items.SelectMany(x => x)));
    }
}

public class Test
{
    public void Main()
    {
        var array = new[] { new Item("a1"), new Item("a2"), new Item("a3") };
        Print.WriteLine(new Item("one"), /* ... */ array, new Item("two")); 
    }
}
Run Code Online (Sandbox Code Playgroud)