C# 中是否有类似JavaScript 的扩展语法的实现?
var arr = new []{
"1",
"2"//...
};
Console.WriteLine(...arr);
C# 中是否有类似JavaScript 的扩展语法的实现?
var arr = new []{
"1",
"2"//...
};
Console.WriteLine(...arr);
没有传播选项。而且是有原因的。
但是,话虽如此,您可以获得具有各种语言功能的类似功能。
回答你的例子:
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;
}
}
}
获得与此类似的行为(无需反射)的一个技巧是接受params SomeObject[][]
并定义一个隐式运算符 from SomeObject
to 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"));
}
}
C# 中没有直接的预构建库来处理 Spread 中内置的内容
为了在 C# 中获得该功能,您需要反射对象并通过其访问修饰符获取方法、属性或字段。
你会做这样的事情:
var tempMethods = typeof(myClass).GetMethods();
var tempFields = typeof(myClass).GetFields();
var tempProperties = typeof(myClass).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
您还可以执行以下操作
var a = new List<int>(new int[]{1,2,3}){5};
Console.WriteLine(a.Count);
将打印 4
如果您想使用随附的可枚举和参数来实现列表或数组的初始化