C#中有任何实现,如JavaScript's spread syntax?
var arr = new []{
"1",
"2"//...
};
Console.WriteLine(...arr);
答案 0 :(得分:12)
没有价差选项。并且有原因。
但是,话虽如此,您可以通过各种语言功能获得类似的功能。
回答您的示例:
C#
var arr = new []{
"1",
"2"//...
};
Console.WriteLine(string.Join(", ", arr));
您提供的链接具有以下示例:
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));
参数 在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));
在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));
反思 在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;
}
}
}
答案 1 :(得分:1)
获得与此类似的行为(无反射)的一个技巧是接受params SomeObject[][]
并定义一个从SomeObject
到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"));
}
}
答案 2 :(得分:0)
C#中没有直接的预构建库来处理Spread内置的内容
为了在C#中获得该功能,您需要反映对象并通过其访问修饰符获取方法,属性或字段。
您将执行以下操作:
var tempMethods = typeof(Program).GetMethods();
var tempFields = typeof(Program).GetFields();
var tempProperties = typeof(Program).GetProperties();
然后遍历并将其放入动态对象中
using System;
using System.Collections.Generic;
using System.Dynamic;
namespace myApp
{
public class myClass
{
public string myProp { get; set; }
public string myField;
public string myFunction()
{
return "";
}
}
class Program
{
static void Main(string[] args)
{
var fields = typeof(myClass).GetFields();
dynamic EO = new ExpandoObject();
foreach (int i = 0; i < fields.Length; i++)
{
AddProperty(EO, "Language", "lang" + i);
Console.Write(EO.Language);
}
}
public static void AddProperty(ExpandoObject expando, string propertyName, object propertyValue)
{
// ExpandoObject supports IDictionary so we can extend it like this
var expandoDict = expando as IDictionary<string, object>;
if (expandoDict.ContainsKey(propertyName))
expandoDict[propertyName] = propertyValue;
else
expandoDict.Add(propertyName, propertyValue);
}
}
}
https://www.oreilly.com/learning/building-c-objects-dynamically
答案 3 :(得分:0)
您还可以执行以下操作
var a = new List<int>(new int[]{1,2,3}){5};
Console.WriteLine(a.Count);
将打印4
如果您想同时实现带有枚举和参数的列表或数组的初始化,则可以使用