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. ...
user avatar
  • 14.6k
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: ...
user avatar
  • 8,498
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 ...
user avatar
  • 14.6k
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 ...
user avatar
  • 16.6k
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 ...
user avatar
  • 43.8k
20 votes
Accepted

Reusability vs simplicity in a small game with a set of interfaces

The problem, however, is that this means when creating my classes, I need to ferry around this TPos generic construct to places where it doesn't make sense to define it. I'm left creating new ...
user avatar
19 votes
Accepted

Generic method to split provided collection into smaller collections

Your implementation is not bad for a no-LinQ solution. But there's always room for improvement. First I'll provide a LinQ solution that provides a clean way to return a chunked list: ...
user avatar
  • 5,553
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 ...
user avatar
  • 314
18 votes
Accepted

Generic, thread-safe MemoryCache manager for C#

Short version: it looks like you're just trying to get a per-type ObjectCache. That's a basic singleton pattern, so I'm unsure what you're trying to gain through all this extra cruft in the class. If ...
user avatar
18 votes
Accepted

VHDL mux in need of generics

There are 4 ways to a solve this: Solution 1: You can define a fixed width STD_LOGIC_VECTOR (SLV), which can be used to construct other vectors (let's call them STD_LOGIC_VECTOR_VECTORs (SLVV). <...
user avatar
  • 587
17 votes

Simplifying a series of type checks and casts in a generic method

You should create a Dictionary<Type,Type> that would associate your object's type with their DTO equivalent, so you wouldn't need any ...
user avatar
  • 12.2k
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...
user avatar
  • 13k
15 votes

Empty Interface usage - Is this a code smell?

It may appear to some as code smell, but it's a practice in use in the .net framework defined as 'marker interfaces'. IReadOnlySessionState is one of these and as ...
user avatar
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 ...
user avatar
  • 43.8k
14 votes

Reusability vs simplicity in a small game with a set of interfaces

Your interfaces are inconvenient, and you are falling in to a trap of convenience that actually leads to inconvenience..... let me explain... C# makes it easy to create the getter/setter methods for ...
user avatar
  • 96.7k
14 votes
Accepted

Instantiating a run-time implementation of an interface

Two parts to this review: the 1-liner for the newInstance() the general mechanism of the method newInstance() This code will work, but only for a subset of Collections. For example, there are many ...
user avatar
  • 96.7k
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 ...
user avatar
  • 6,418
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 ...
user avatar
  • 43.8k
13 votes
Accepted

Generic cached value class mimicking Lazy<T>

First off, never lock on this. You never know who else is locking on that instance. Conversely, clients don't know that the cache is locking on itself either. For ...
user avatar
  • 1,167
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 ...
user avatar
  • 5,714
12 votes

Simple generic double buffer pattern

Please correct me if I am wrong, but isn't such scenario possible with accepted answer? ...
user avatar
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(\...
user avatar
  • 25.3k
11 votes
Accepted

Empty Interface usage - Is this a code smell?

An empty interface is a code smell. In C# you can use attributes to mark a class instead of making it implement an empty interface. As I suggested in my comment, you can use reflection to build your <...
user avatar
  • 6,557
11 votes
Accepted

Generic deque implementation

This class looks reasonably complete, and fully usable. There are some issues I see that may affect the experience though, and also some suggestions about a better way to do it... Issues: Why do you ...
user avatar
  • 96.7k
11 votes
Accepted

Cutting it to Pages

Compiler Warning This line gives you a compiler warning because it's not using generics: List activeList = listOfLists.get(pageNumber); it should be ...
user avatar
11 votes
Accepted

How can I make this StringToCollections method more generic?

Consistency: Don't mixup using var and declaring your variables explicitly. Choose one of the two, preferably var. Combining ...
user avatar
  • 5,553
11 votes
Accepted

Simple Generic output for Deserializer

Here are a few remarks. I'm not perfect or write perfect code but I hope these tips will help write better code: Naming consistency: It would be easier to understand if the names of your two methods ...
user avatar
  • 5,553
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 ...
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() ...
user avatar
  • 6,121

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