3 ms·
It's been a while since I last wrote C# code, does it really allow for heterogeneous value types? If so, that's cool. However, from my recollection, the C# API
by Udo 11y ago
It's been a while since I last wrote C# code, does it really allow for heterogeneous value types? If so, that's cool. However, from my recollection, the C# API itself is all about opaque objects and behaviors. That's not a criticism of the paradigm, and I remember liking the C# standard libs and structures a lot better than Java's, but it's not something I would equate to generic heterogenous containers in dynamic languages. It's not in question whether you can replicate some of that in .NET, it's more about whether that's a core language idiom or not.
Edit: `collections.namedtuple` is Python, mixed that up.
- jstimpfle 11y agopython, not C#
- radicalbyte 11y agoC# doesn't have real tuples, it has a set of pre-made generic (template) implementations supporting up to around 15 arguments iirc. There is currently active discussion on github about adding true language-level support for tuples of arbitrary length.
- mike_hearn 11y agoI must admit, the idea of an un-specialised tuple with 15 elements is pretty damn scary. If I saw that in a codebase it'd be a real WTF moment. After three or four fields you should really be naming them, and if your language makes defining small classes painful, it's better to fix that with a Scala-type case class syntax (or data classes in Kotlin).
- jdmichal 11y agoThose parameterized Tuple types are from the olden days of before LINQ. Now C# has anonymous types, which can handily replace them. In the end, these are compiled down to classes with read-only properties, so it's almost no pain to implement such classes. var v = new { Amount = 108, Message = "Hello" }; Console.WriteLine(v.Amount + v.Message); https://msdn.microsoft.com/en-us/library/bb397696.aspx https://msdn.microsoft.com/en-us/library/bb397696.aspx
- douche 11y agoThe problem with anonymous types is any time you have to come back up from any inner scope that you are inside of. Fortunately, stuff like ReSharper makes it easy to transform an anonymous class into a real one, but then you have to battle an explosion of DTO objects, unless you are ruthless about consolidating similar but not-quite-the-same data together. That being said, anonymous types are a godsend for doing EF and LINQ stuff, where the scope is limited. Beats the hell out of the old ADO.NET stuff. Also for easily creating garbage to get serialized for JSON returns to front-end web code.
- jdmichal 11y agoSure; the side effect of being "anonymous". (Which really just means that you can't write code to address it directly; it's a perfectly normal .NET class under the hood.) But you hit it exactly that they address that transitional pain point between addressable object and serialization format. I tend to specifically not pare down data objects that are "similar-but-different", as I usually find it's nothing but a pain point later when they evolve to look more "different-but-with-a-couple-similaries". Easier overall to just call different things different names from the start.
- jdmichal 11y agoIt's hard to tell exactly what GP is referring to. It could be the parameterized Tuple type, which has built-in implementations for up to 8 fields. But those will all just be named Item1, Item2, etc. https://msdn.microsoft.com/en-us/library/dd383325%28v=vs.110%29.aspx https://msdn.microsoft.com/en-us/library/dd383325%28v=vs.110... The conversation makes me lean more towards C# anonymous types, which are just syntactic sugar for creating classes with public, read-only properties. But they feel like creating named, statically-typed tuples. https://msdn.microsoft.com/en-us/library/bb397696.aspx https://msdn.microsoft.com/en-us/library/bb397696.aspx
- nine_k 11y agoThis is indeed what I referred to. Should work great with `var`. Anonymous types are probably even better.