JavaScript syntax is common in C #

Is there any implementation in C #, for example, the common JavaScript syntax ?

var arr = new []{ "1", "2"//... }; Console.WriteLine(...arr); 
+25
source share
3 answers

There is no possibility of distribution. And there are reasons for this.

  1. Properties are not an array in C # unless you use the params keyword
  2. Properties that use the param keyword must either:
    1. Share the same type
    2. Have a common shared type, such as double for numbers
    3. Be an object of type [] (since the object is the root type of everything)

However, by saying this, you can get similar functionality with different language features.

Answering your example:

WITH#

 var arr = new []{ "1", "2"//... }; Console.WriteLine(string.Join(", ", arr)); 

The link you provide has the following example:

JavaScript Distribution

 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)); 

Params In C # with the same type

 public int Sum(params int[] values) { return values.Sum(); // Using linq here shows part of why this does not make sense. } var numbers = new int[] {1,2,3}; Console.WriteLine(Sum(numbers)); 

In C # with different numeric types using double

 public int Sum(params double[] values) { return values.Sum(); // Using linq here shows part of why this does not make sense. } var numbers = new double[] {1.5, 2.0, 3.0}; // Double usually does not have precision issues with small whole numbers Console.WriteLine(Sum(numbers)); 

Reflection In C # with various numeric types using object and reflection, this is probably the closest thing to what you are asking.

 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; } } } 
+13
source

there is no direct built-in library in C # to handle what is built into Spread

To get this functionality in C #, you need to mirror the object and get methods, properties or fields by their access modifiers.

You would do something like:

 var tempMethods = typeof(Program).GetMethods(); var tempFields = typeof(Program).GetFields(); var tempProperties = typeof(Program).GetProperties(); 

then iterate and add them to your dynamic object:

 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

0
source

One trick to get behavior like this (without reflection) is to accept params SomeObject[][] and also define an implicit statement from SomeObject to SomeObject[] . Now you can pass a mixture of SomeObject arrays and individual SomeObject elements.

 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")); } } 
0
source

Source: https://habr.com/ru/post/1257758/


All Articles