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. ...
32
votes
Extension method replacing elements from an IEnumerable<T> collection
Why didn't Microsoft include this in the .NET framework?
Probably because it can be realized in one line with Select:
...
31
votes
Accepted
A Failable<T> that allows safe returning of exceptions
First: congratulations, you have rediscovered the error monad.
https://hackage.haskell.org/package/mtl-2.2.1/docs/Control-Monad-Error.html
Second: as noted in the comments, C# already has the ...
28
votes
Accepted
Simple generic cache
You say cache I say memory leak.
You haven't given much thought to your eviction policy yet and that's 95% of the work in writing a decent cache. Your expired entries are only removed when someone ...
25
votes
Accepted
Extension method replacing elements from an IEnumerable<T> collection
I cannot comment about the speed because I didn't benchmark it but there are two obvious improvements.
add an IEqualityComparer<T> as a third parameter so ...
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 ...
16
votes
Accepted
Convert a boxed integer to an nullable integer with potentially different type
You should probably add IConvertible restriction on T, since it is used by Convert.ChangeType...
15
votes
Convert a boxed integer to an nullable integer with potentially different type
I'm not sure if you understand what boxing means because you use an int in the example.
Boxing is the process of converting a value type to the type ...
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
Accepted
Binary Heap where a comparison delegate is used
Performance bug
private void BubbleUp(T element)
{
int elementIndex = _elements.IndexOf(element);
...
This is \$O(n)\$, which completely oblitirates \$O(\...
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 ...
9
votes
Structure to ByteArray Extension
I haven't used it, however all of the documentation seems to suggest that you need explicitly free the memory allocated through:
...
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
Return a key when a function returns true
Needs more LINQ:
return classifications.FirstOrDefault(item => item.Value.Invoke(input)).Key;
This has the advantage (IMO) of highlighting the fact that you'...
8
votes
Accepted
Removing exact instances of elements in one list from another
Additional to the valid point from Rick Davin:
1) You can drop the Contains because IndexOf returns -1 if the item is not ...
8
votes
Accepted
Return a key when a function returns true
Three small things:
A Func<TValue, bool> is more commonly known as a Predicate<TValue>
You're mapping ... the "...
8
votes
Accepted
Generic immutable object builder
I would prefer the following syntax for its compile-time type-checking:
...
8
votes
Accepted
Automatic computation of mean, standard-deviation, sum, min, max in property
You can add additional constraint to your generic type :
public abstract class MagicProperty<T>
where T : struct, IComparable
It doesn't makes ...
8
votes
Binary Heap where a comparison delegate is used
You added beginner, so I'm going to give you a few pointers to start with, to help you work from.
First: C#6.0 (which I see you are using) allows void methods to ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
generics × 501c# × 234
java × 129
object-oriented × 37
c × 35
.net × 31
c++ × 30
reflection × 25
design-patterns × 23
extension-methods × 21
linked-list × 20
swift × 20
array × 19
performance × 18
collections × 17
interface × 16
type-safety × 15
lambda × 14
algorithm × 13
rust × 13
inheritance × 13
hash-map × 12
serialization × 12
beginner × 11
tree × 11