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 ...
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 ...
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:
...
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 ...
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 ...
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).
<...
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 ...
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
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 ...
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
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 ...
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 ...
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
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 ...
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
Simple generic double buffer pattern
Please correct me if I am wrong, but isn't such scenario possible with accepted answer?
...
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
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 <...
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 ...
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
...
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 ...
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 ...
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() ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
generics × 480c# × 233
java × 128
object-oriented × 35
c++ × 31
.net × 30
c × 25
reflection × 25
design-patterns × 22
extension-methods × 22
linked-list × 21
swift × 19
performance × 18
array × 18
collections × 18
interface × 17
type-safety × 14
inheritance × 13
serialization × 13
lambda × 13
beginner × 12
hash-map × 12
algorithm × 11
tree × 11
linq × 11