C# Stable Sort

9 Comments »

A sort is stable if the original order of equal elements is preserved. An unstable sort does not guarantee that equal elements will appear in their original order after sorting.

The Sort methods used by the .NET collections are unstable. These sort methods, which include System.Array.Sort and System.Collections.Generic.List<T>.Sort, use the QuickSort algorithm, which is relatively fast but in this case, unstable. However, there may be instances where you require a stable sort, so a custom solution is required.
Read the rest of this entry »

Property Delegates with Anonymous Methods

7 Comments »

No programming language is perfect. In spite of the many strengths of C#, there are still a few gaping holes, including generics variance and property delegates.

A delegate is a reference to a method. Unfortunately, C# currently does not support property delegates, in other words, a reference to a property. But there are a few workarounds, one of which involves anonymous methods.

An anonymous method is a new feature in C# 2.0 that enables you to define an anonymous (nameless) method called by a delegate. Anonymous methods are perfect when there is no need for multiple targets, and the code is relatively short and simple. They also come in handy when you need a delegate to a property.

Read the rest of this entry »

Convert Between Generic IEnumerable

4 Comments »

Generics in .NET 2.0 provides the ability to create strongly-typed collections in C#. Unfortunately, C# currently does not support generics variance, which would allow inheritance of generic types.

For example, consider a list of strings and a list of objects:

List<string> strings = new List<string>();
strings.Add( "hello" );
strings.Add( "goodbye" );
List<object> objects = new List<object>();
objects.AddRange( strings );

The final line in the code above generates a compiler error. But why? Since the ‘string’ class derives from the ‘object’ class, one would expect List<string> to also implicitly derive from List<object>. This capability is called generics variance, but C# currently does not support it.

Fortunately, you can brute force your way to a solution by creating a generic ConvertIEnumerable method:

Read the rest of this entry »

SecureString: Safe from Forensics, but not Surveillance

5 Comments »

The SecureString class is new in the System.Security namespace with the .NET v2.0 upgrade. SecureString keeps sensitive data encrypted in memory so that it cannot be easily stolen. SecureString plugs a specific security hole but does not guard against all threats while securing information in applications.

Read the rest of this entry »

Padding: Like a Rectangle, but Not

3 Comments »

The .NET 2.0 upgrade included many minor improvements that are easily overlooked. One such improvement is Padding, a handy structure in the System.Windows.Forms namespace that you may find useful for representing offsets, margins or padding in the user interface.

Read the rest of this entry »

Simplify Delegates with Inferences

No Comments »

Delegates can be tricky to understand. Think of a delegate as a reference to a method (pointer to a function).

Read the rest of this entry »

Count Items in a C# Enum

4 Comments »

You can count the number of constants defined in a C# enumeration with the static Enum.GetNames method. This returns an Array containing the names of the constants defined in the enumeration. You can then check the Array’s Length property.

Read the rest of this entry »

In C#, a string is a String

1 Comment »

C# includes a number of pre-defined “built-in” data types. Each built-in data type is represented by a class in the System namespace that inherits from the base System.Object class. For example, an integer is represented by the System.Int32 class, and a string is represented by the System.String class.

C# defines an alias keyword for each built-in type. The alias keyword and its corresponding C# type are interchangeable. For example, you can define a string with the “string” keyword or “System.String” type:

Read the rest of this entry »

C# String Tips

90 Comments »

The .NET string class is quite comprehensive, yet some common string functions are missing or not entirely obvious. This article provides quick tips on using .NET strings.

Read the rest of this entry »

ASCII Table

6 Comments »

Did you know? You can type ASCII characters into any application. Press and hold the Left-Alt key, then using the numeric keypad, type the four-digit decimal number for the ASCII character you want, then release the Left-Alt key. For example, to type è, press and hold Left-Alt, then type 0232 on the numeric keypad. When you release the Left-Alt key, è will be typed.

Here is the world famous ASCII table:

Read the rest of this entry »

« go backkeep looking »