Extract Application’s Own Icon in C#

4 Comments »

It’s easy to extract an application’s own icon in C#:

form.Icon = Icon.ExtractAssociatedIcon( Application.ExecutablePath );

The ExtractAssociatedIcon static method on the Icon class (in System.Drawing) extracts the associated icon from any file.  By supplying the Application.ExecutablePath (in System.Windows.Forms), you extract the application’s own icon.

C# Copy Folder Recursively

48 Comments »

Sadly there is no built-in function in System.IO that will copy a folder and its contents.  Following is a simple recursive algorithm that copies a folder, its sub-folders and files, creating the destination folder if needed.  For simplicity, there is no error handling; an exception will throw if anything goes wrong, such as null or invalid paths or if the destination files already exist.

Read the rest of this entry »

C# Read Text File into String

9 Comments »

Here is the easiest way to read an entire text file into a C# string:

string s = System.IO.File.ReadAllText( path );

Do Not Return Private C# Arrays

2 Comments »

Be careful when returning C# arrays that are private members of an object, as it essentially makes the array contents public, since the client can easily change the array.

Read the rest of this entry »

C# Empty Enumerator

11 Comments »

This article provides C# code for an empty enumerator.  This generic class can be used to simulate enumeration over an empty collection of any type of objects.  Here is the code:

Read the rest of this entry »

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 »

« go backkeep looking »