Generic Class: Duplicate Method Overloads

4 Comments »

When two overloads of a method in a generic class are the same (have identical type arguments) as a result of the generic type you specified, which method is called?

Read the rest of this entry »

Nested Generics

3 Comments »

Given two generic classes:

public class Type1<T> {}
public class Type2<T> {}

.NET allows you to specify a generic type as the type of another generic type:

Type1<Type2<int>> obj = new Type1<Type2<int>>();

Read the rest of this entry »

Multiple Generic IEnumerable

5 Comments »

It’s possible to provide multiple generic enumerators for a single class.  The trick is that clients must specify which enumerator to use.

Read the rest of this entry »

Parse and Sort Comma-Delimited Numbers

4 Comments »

Here is one way to parse and sort a string with comma-delimited numbers:
Read the rest of this entry »

C# Object Clone Wars

24 Comments »

Cloning C# objects is one of those things that appears easy but is actually quite complicated with many “gotchas.” This article describes the most common ways to clone a C# object.

Read the rest of this entry »

Manipulating Controls Across Threads

11 Comments »

Methods that affect a Windows Forms control can be executed only on the thread that created the control. .NET does not permit directly manipulating controls across threads. The Visual Studio compiler under .NET 2.0 will mark these attempts as errors. .NET 1.1 will allow them, but these will often result in unexpected behavior like incorrectly-painted controls.

Read the rest of this entry »

Convert Handle to Form, and Vice Versa

3 Comments »

This is one of those things that’s obvious once you know it.  When dealing with COM, there is often a need to convert a WinForms Form object to an IntPtr handle, and vice versa.

Read the rest of this entry »

Enumerate Collections without Exceptions

2 Comments »

It’s important to note that an enumerator does not have exclusive, thread-safe access to its collection.  Even when a collection is synchronized, other threads can still modify the collection.  Therefore, a collection’s contents can change while enumerating through it, which will cause the enumerator to throw an exception.  So there are three key ways to safely enumerate a collection:

Read the rest of this entry »

Hide Visual Studio Macro Balloon

3 Comments »

When you execute a macro in Visual Studio, a balloon pops up in the system tray to indicate the macro is running, and it’s accompanied by the typical balloon “pop” sound.  The problem is on most fast development PCs, the balloon is visible for only a fraction of a second and therefore of little use.  And the pop sound can become annoying if you use macros extensively.

Read the rest of this entry »

Grow Your Own SyncRoot

3 Comments »

Multi-threaded code is challenging to get right and even harder to debug once it’s gone wrong. This is especially true when attempting to collect data from multiple threads. To make this easier, many .NET collection classes include the SyncRoot property to maintain proper synchronization with other threads that might be simultaneously modifying the collection. But then Microsoft changed its mind in .NET 2.0 and decided to let the developer decide how to manage synchronization, and so none of the new generic collections have a SyncRoot property.

Read the rest of this entry »

« go backkeep looking »