4 ms·
Ask HN: Can we just take a moment to appreciate how elegant C# is?
var list = Enumerable.Range(0, 20).Select(i => new { Foo = i, Bar = "hey" });
This is beautiful!
- asadawadia 5y agoKotlin to get a List<Pair<Int, String>> val list = (1..20).map { Pair(it, "hey") }
- uejfiweun 5y agoFor my understanding, what is happening in "new { Foo = i, Bar = "hey" }"? Is that some type of tuple or struct or something? Anyway I agree, I like C# for the same reasons I like Java. Very readable but still powerful.
- pipeline_peak 5y agoThat is an anonymous type. They cannot be returned explicitly, but you can instead use "(Foo: i, Bar: "hey")" to return a tuple with properties (int Foo, string Bar)
- jbjbjbjb 5y agoThis is Linq (and anonymous types). There are ports of Linq for other languages and a lot languages have similar features. It is nice though.
- trinovantes 5y agoLinq is definitely my favorite feature of C#
- neximo64 5y agoJavascript does that too with its ES6 chaining syntax, would you say it is elegant too? var list = Array.from({length:20}, (_, i)=>({"Foo":i+1, "Bar": "hey"}))
- pipeline_peak 5y agoElegant, I would say so myself. Does the underscore represent the list?
- JohnCurran 5y agoIn Elixir: list = Enum.map(1..20, & %{foo: &1, bar: "hey"}) Not meant to be a comparison, just another example
- vips7L 5y agoIsn't this every language now? Apart from the anonymous type even Java can do it elegantly: var lst = IntStream.range(0, 20) .map(i -> Pair.of(i, "hey")) .toList();
- humbleMouse 5y ago
- e_coli 5y ago~ 40 years ago, Smalltalk had: (1 to: 20) collect: [ :each | Array with: each with: 'hey' ] And Smalltalk got it from Lisp. Just saying for comparison. Edit: forgot to mention - the syntax is entirely consistent and unlike Linq does not seem bolted on to the rest of the language.