Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

You can take a look at C# generics. They are quite simple.


sort by: page size:

You really ought to check out Linq. C# generics and lambdas are really nice too.

Check out C# and LINQ. They pull it off with anonymous types.

This is why I like C# so much, you can go from LINQ & generics to pointers. So much versatility.

Kind of like arrays in C or in pre generics Java and C#. Definitely not ideal but not that unusual either.

I tried your search, all results are about C# classes, generics etc.

Not sure if C# can do that, but java’s implementation can return an arbitrary object based on the templates/parameters.

That's an option, but it requires a significantly more complex generics runtime.

C# has an is operator as well. I found it useful with generics.

mono and .net are no better at such situations. also, if you want value types, there is scala.

Ah, this is exactly the approach I took in C# (though with a small range of branded types to stand in for numbers). Thanks for clearing this up!

And that has a baggage of problems. The best way is what C# does, which incidentally is also what Go does with maps:https://dave.cheney.net/2018/05/29/how-the-go-runtime-implem...

This is essentially how C# generics have worked since forever. If you want performance, don't use pointer type arguments.

Generics, LINQ, structs, delegates, to name just four.

That example, in a statically-typed language could be handled with overloading or polymorphism, which are both far more concise IMO.

C#'s standard library, for example, has common interfaces for collections so the code will still work on all kinds of lists, arrays, etc.


Yes and you do things in Java like create an MyIntCollection to get around the shortcomings of generics wrt. primitives, but the point is that it is just that: workarounds.

What I really want is for perf to be good while the code is still clean/readable/expressive/idiomatic. In Java/C# you must usually write idiomatic+slow and then refactor into fast kludges.


I just love the generic 'List' class from C# - a modern data structure.

It does seem you can handle them, but we .NET people are lazy (as in, Larry Wall lazy http://en.wikipedia.org/wiki/Larry_Wall ).

I agree with the poster's reply to that: "Yes I agree that this is simple and fast, but.. What I really want is a 1-D list of my own object types." I get that when using XML web services with .NET (though mostly when consumed with other .NET or similar languages !!! which goes against it being an universal data transfer language )


Very cool. Anonymous types can be really handy in C#, especially when dealing with LINQ to objects code. This looks like it could be a similar fit.

Record types introduced in C# 9 solves this.

It's now as simple as

record SomeData(string Name, int Id);

Which gives you an immutable object, auto generated constructor, and some additional helpers to make life easier.

next

Legal | privacy