39
votes
Own implementation of Lazy<T> object
is the current implementation 'safe'?
Absolutely not. The fact that you had to ask this question indicates that you do not understand enough about threading to build your own mechanisms like this. You ...
19
votes
Accepted
Own implementation of Lazy<T> object
Having backported Lazy to .NET 2.0, I want to offer a variant...
First of all, this looks a lot like LazyInitializer. If that works for you, then there is no need to go about creating a ...
14
votes
Accepted
Heap implementation for numeric types
Memory management
You never delete[] arr;, which leaks memory. Not a good thing! There are multiple ways to fix this:
Adding correct calls to ...
14
votes
Own implementation of Lazy<T> object
is there anything else I'm missing that I should be concerned about in a high traffic application?
Yes, your Lazy<T>.Value isn't generic anymore but an ...
13
votes
Accepted
Copy a similar list to another
Small things
Your code styling is generally good, but a few possible improvements:
You can add a second generic type parameter to your method to avoid the need to cast to ...
12
votes
Generic C++ Class to Associate enum Values with Strings for Translation
Consider using magic enum instead. It basically automates this.
Alternatively, for example if you want other spellings for the string representations, I found X-macros very useful:
...
11
votes
Accepted
Take a desired string, iterate through objects to see if it exists in a given field and append a number until a unique string is found
There's certainly room for improvement.
Move that that Select(stringFieldFunction).ToList() out of the while loop. Iterating an ...
11
votes
Accepted
Method for create a copy of List<T>
Given that arrayCopy has exactly one line, it's not clear that there's much value in having it extracted as a method.
toArray() ...
10
votes
Copy a similar list to another
for (int i = 0; i < propInfos.Length; i++)
Any particular reason why you're not using foreach (var prop in PropInfos) here?
<...
10
votes
10
votes
Own implementation of Lazy<T> object
Meaning that I have to put half of my logic concerning the lazy property in the constructor, and having more boilerplate code.
This is a little speculative, but I think you have an XY problem. You're ...
10
votes
Accepted
Generic C++ Class to Associate enum Values with Strings for Translation
It's not going to be perfect
Unfortunately, until we get true enum reflection in C++ (supposedly in C++26), any solution to map enum values to strings is going to have some issues. Your solution ...
9
votes
Declarative type comparer
Very nice implementation; I always like seeing your code here. I really only have five very minor opinions on this implementation:
I'm not sold on expr as an ...
9
votes
Generic data structures in C
- float
float constants need the suffix f:
...
9
votes
Accepted
Classes representing 2D points and pixels
A fine example how modern C# can make your life so much easier!
But first things first:
Errors and maybe errors
Your GetHashCode() is realy weak! Just take two example points (1|1) and (0|32) and ...
8
votes
Accepted
Generic Dictionary Equality Comparer
You said you built this specifically so you can use dictionaries as keys in other dictionaries?
In that case, you've got a problem. Consider the following demonstration:
...
8
votes
Generic linked list implemented in pure C
GenericList.h and GenericList.c
Generally this is excellent code as long as it is a stack you want. It seems that what is created is really a generic stack implemented on a linked list rather than a ...
8
votes
Accepted
Generic matrix library in Java
Dense matrix multiplication in 15572 ms.
Going by the demo code I assume this was the product between two 1000x1000 matrices. By my estimate based on old code the raw operations by themselves could ...
7
votes
Heap implementation for numeric types
An addendum to hoffmale's excellent answer (read and upvote that one first!):
Prefer to initialize members, rather than assign them during construction:
...
7
votes
Accepted
Immutable builder and updater
You use IList<> where you should use ICollection<>. I've rarely encountered a scenario where ...
7
votes
Generic Heap Implementation in C#
The code in question is easy to read and understand.
You have named your variables and methods well, using the recommended naming and casing styles.
The code in question is well documented as well.
...
7
votes
Generic Heap Implementation in C#
Nice Code!
One simple suggestion. Add a constructor with capacity and initialize the List with that capacity.
If you don't have the capacity preinitialize everytime you Add a new element to the list ...
7
votes
Accepted
generic implementation of median
It modifies the input
I think it is surprising that an operation that is intended to just give you some statistics back is going to modify the input. In the test code you also test it on ...
7
votes
An Attempt at Creating Generic Min()/Max() for Fundamental Types
The compound expression statement would restrict the scope of x and y, but there may still be conflicts with variable names that ...
6
votes
Accepted
Collection with Generic Index
The problem
There is a massive hole in your logic here. You are relying on the fact that there exists a property whose type is equal to the name of the property.
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
generics × 507c# × 232
java × 130
object-oriented × 37
c × 36
c++ × 34
.net × 31
design-patterns × 23
reflection × 23
extension-methods × 21
linked-list × 20
swift × 20
array × 19
performance × 18
collections × 17
type-safety × 17
interface × 16
lambda × 14
algorithm × 13
inheritance × 13
hash-map × 12
rust × 12
serialization × 12
beginner × 11
tree × 11