Skip to main content
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. ...
Eric Lippert's user avatar
  • 14.9k
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: ...
JanDotNet's user avatar
  • 8,538
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 ...
Eric Lippert's user avatar
  • 14.9k
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 ...
RobH's user avatar
  • 17k
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 ...
t3chb0t's user avatar
  • 44.3k
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 ...
Theraot's user avatar
  • 314
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...
Nikita B's user avatar
  • 13k
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 ...
t3chb0t's user avatar
  • 44.3k
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 ...
hoffmale's user avatar
  • 6,508
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 ...
t3chb0t's user avatar
  • 44.3k
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 ...
Ben Aaronson's user avatar
  • 5,734
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(\...
coderodde's user avatar
  • 29.1k
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 ...
Pieter Witvoet's user avatar
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() ...
Eric Stein's user avatar
  • 6,562
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? ...
Snowbody's user avatar
  • 8,462
10 votes

Turning a byte array into a C# object whose type is unknown at compile time

...
t3chb0t's user avatar
  • 44.3k
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 ...
jpmc26's user avatar
  • 1,233
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: ...
forsvarir's user avatar
  • 11.7k
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 ...
Jesse C. Slicer's user avatar
9 votes

Generic data structures in C

- float float constants need the suffix f: ...
alx - recommends codidact's user avatar
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 ...
Matthias's user avatar
  • 106
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'...
Mathieu Guindon's user avatar
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 ...
JanDotNet's user avatar
  • 8,538
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 "...
Vogel612's user avatar
  • 25.3k
8 votes
Accepted

Generic immutable object builder

I would prefer the following syntax for its compile-time type-checking: ...
Blorgbeard's user avatar
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 ...
Denis's user avatar
  • 8,508
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 ...
Der Kommissar's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible