Tag Archives: Code

C# Generics

I’ve been playing around with generics in C# today. Simply put,
generics are akin to templates in C++, only much better. My aim in this
post is not to explain the in’s and out’s of generics, in the new 2.0
version of the .NET Framework but to highlight some key points of

For a primer on generics, read part 1 and part 2 of an article included in MSDN magazine, by Jason Clark.

: So, what’s the point of generics?
A: They provide a mechanism for defining code without specifying variable types.

So, the typical example is the System.ArrayList problem. Let’s say that
you want to store integers in an ArrayList because it offers dynamic
sizing, sorting and the ability to remove random accessed elements from
the collection. The problem is that ArrayList uses a base type of
System.Object to store data – a reference type. Adding an integer to an
ArrayList will involve boxing the integer value type before insertion,
which incurs a large performance overhead. Casting to
System.Object also side steps type checking, so the compiler will not help
you out if there is problem with instances of incorrect types added to
the collection, which means developers have to add their own type
checking code.

Generics provide a neat way to define collection classes
(but not restricted to collection classes), which are type agnostic.
The following example defines two instances of the System.Collections.Generic.List class, one
which contains strings, and the other which contains integers.

List stringList = new List();
List intList = new List();

: How do generics work in the CLR?

In C++, templates are processed by the compiler for each use of the
template. Each template is expanded into a concrete type at compile time when required
by a caller of said template. So, using a single template multiple
times with different types will create a new concrete version of the
template for each type. Misuse of templates in C++ can very quickly cause code bloat.

C++ templates are not compiled or type checked for errors until they
are used. So, the compiler will let you write reams of code, and won’t
compile any of it until it is specifically used as a template

No validation of template code is performed by the C++ compiler. The
example below is a template function to compare two instances of type
‘T’. This function works well when ‘T’ is an integer, but what if ‘T’
is a class type
that does not contain the less than operator? The developer will see
some ambiguous compiler error in the template, not an error indicating
the real problem.

class Foo
int IsLessThan(T one, T two)
return one < two;

Generics deal with both code bloat and type validation checking. Code
bloat is limited because the JIT compiler stores one version of the
common reusable code between template instances. Expansion of generic types is
performed at run-time, all that exists of a constructed type at compile
time is a type reference.

Generics are type and validation checked at compile time using
constraints.  In their bare form (without constraints), generics
don’t enable the developer to do much. The
example below is the C# version of the C++ template above. Compiling
this code will cause an error.  The compiler doesn’t know anything
about type ‘T’ and cannot deduce whether a less than comparison is
legal, so it doesn’t permit it. The same goes for the CompareTo method.
In fact, pretty much the only operations and method calls that can be
performed on a bare generics are those associated with
System.Object – GetType, ToString etc.

public static bool IsLessThan(T one, T two)
return one < two;

Q: So what are these constraints?

The following example is the correct way to achieve the comparison we were looking for above. The where
keyword is followed by the constraints, in this case I have indicated
that ‘T’ must be derived from the IComparable interface, so the compiler knows that
types passed to the generic method can always be compared. 
Constraints aid the compiler in describing what rules can be applied to
parameter types, and can determine the operations and
methods that can be called in generics.

public static bool IsLessThan(T one, T two) where T:IComparable
return one.CompareTo(two) < 0;

Another constraint is new(). This constraint tells the compiler
that the code in the generic method can create new object instances of
the parameter type.  This is especially useful when using helper
classes to construct derived class types.  I ran into this
constraint today when I was developing a utility class.  I was
surprised to find out that the new() constraint only enables
construction of types that have a default non-parameterized
constructor.  At the time I didn’t know why this was so, but I
later found out it is because the compiler cannot evaluate
parameterized constructors without breaking validation
checking.  For example, if ‘T’ is a class, which requires a
parameter passed to the constructor of ‘T’ that is itself complex type, the compiler
has no way of knowing if the generic will work for all different types
bound to ‘T’. The template may work great if ‘T’ is an employee, with
parameterized constructor taking in a name as string, but  would not work if
‘T’ is then an integer.  My example below highlights this

using System;
using System.Collections.Generic;
using System.Text;

namespace Generics
/// Base class, nothing fancy, just stores a name.
class MyBase
string _name = String.Empty;

public string Name
get { return _name; }
set { _name = value; }

public MyBase(string name)
_name = name;

public MyBase()
_name = “no name”;

/// Sub class, says hello.
class MySub : MyBase
public void Hello()
Console.WriteLine(“Hello ” + Name);

/// Utility class.
class Program
/// Create a version of the base class the old way.
public static MyBase NewInstance_Bare(string name)
return new MyBase(name);

/// Create an instance of any class that derives from MyBase.
/// The new() constraint enables the method to create a new instance
/// using the default parameter-less constructor of T.
public static MyBase NewInstance_Template(string name) where T:MyBase, new()
// T instance = new T(name); — this will produce a compiler error.
// The compiler has no way of knowing that all types ‘T’ will have a
// constructor with string as a single parameter.
T instance = new T();
instance.Name = name;
return instance;

static void Main(string[] args)
// No need to store instance as MyBase type and then cast up,
// this is just to show that the helper method will create an
// instance of MySub, which is derived from MyBase.
MyBase instance = NewInstance_Template(“Rob”);

Q: What is inference?

.Net is smart enough to determine the parameter type in a generic if
variable of the same type is passed as to the method as a parameter.
The example code below defines a generic method. When calling this
method, type ‘T’ can be inferred by the parameters passed to the method.

public static bool IsLessThan(T one, T two) where T:IComparable
return one.CompareTo(two) < 0;

static void Main(string[] args)
int a = 1, b = 2;
// ‘T’ is inferred as int because a and b are of type int.
bool result = IsLessThan(a, b);

Common Table Expressions

Today I was delighted to learn about common table expressions in SQL Server 2005. Essentially, CTEs replace the need for cursors when executing statements against rows in a result set. Here are some examples…

Let us assume that we have an employee table, and the employee table lists all employees in the company. Each employee has a unique ID – EmployeeID, which is the primary key in the table. Each employee reports to one manager and this is indicated by the managers EmployeeID in the ManagerID column for a given employee. e.g

EmployeeID Name ManagerID
1 Bob (CEO) NULL
2 Sally 1
3 Jim 1
4 Paul 2
5 Simon 4

As shown above, Bob is the CEO and reports to nobody, Sally and Jim report to Bob, Paul reports to Sally, and Simon reports to Paul.

Let’s say that we want to list how many employees report to each manager. We could write a query using the group by clause (assuming we don’t want to join the result set back with the employees table to match the name with EmployeeID), as follows:

ManagerID, count(*)

What if we wanted to display those managers that had more than one person reporting to them? This result could be obtained by storing the results of the previous query in a temporary table and then running a separate query, or using a cursor. However, using a CTE it is possible to gain the same result with one statement, as follows:

WITH my_CTE (ManagerID, Employees) AS
e.ManagerID, count(*) AS Employees
#employees e
GROUP BY e.ManagerID
SELECT e.[Name], cte.*
my_CTE cte
INNER JOIN #employees e ON e.EmployeeID = cte.ManagerID
Employees > 1

How about listing the company hierarchy in as a tabular list, indicating rank, starting with the CEO, and following with the direct reports at each level throughout the company? A CTE is great for this problem:

WITH my_CTE (ManagerID, EmployeeID, Rank) AS
e.ManagerID, e.EmployeeID, 0 AS Rank
#employees e
e.ManagerID IS NULL
e.ManagerID, e.EmployeeID, cte.Rank + 1
#employees e
INNER JOIN my_CTE cte ON e.ManagerID = cte.EmployeeID
e.[Name], cte.*
my_CTE cte
INNER JOIN #employees e ON e.EmployeeID = cte.EmployeeID

The last example uses recursion to produce the ranking. This query will work no matter how many employees work for the company and no matter how many levels of management. Prior to CTEs, obtaining the same result would have involved some sophisticated SQL.

Are you a smart programmer?

…. If you are you might want to take a look at the following job advertisement from Ontology Works.

As a software developer I always become suspicious of any job that
advertises “programmer” positions and not “developer” positions (see blog post on Programmer vs
Thoughts of a desk behind the company coffee vending machine, a small
desktop, and a “how to program in C” manual spring to mind when
employers use the term “programmer”. As if this isn’t bad enough,
Ontology Works is looking for a “hacker”, now if this doesn’t send most
moderately talented developers running for the hills then I don’t know
what will.

Note: If you know the “right” thing when you see it, you’ll probably realize that this job isn’t it.

I am intrigued in what it would take to get one of those Ontology Works
mugs, given that even Ontology Works does not have them. I’m guessing
the person who ordered them was not too “smart”.

Ontology Works seeks a programmer to help push forward its cutting edge ontology-based information management products.

What you know is less important than how smart you are, but the following skills will help:

– Java (kind of a requirement, really. A smart hacker like you could
pick it up fast, but a smart hacker like you probably already knows

– C/C++/C#

– Prolog/Logic Programming

– Web Ontology Language/Semantic Web

– Relational databases

– Non-relational (OO, OR, etc) databases

– Database theory & implementation

– Distributed & parallel algorithms

– Linux

– GUI programming

– JSP/HTML/Tomcat

– The ability to decipher academic papers

– The ability to write academic papers

– The ability to write cogent documentation

– The ability to recognize the Right Thing when you see it.

– Even trickier: the ability to work with clients.

– Any of the usual round of acronyms (WSDL, J2EE, SOAP, XML, UDDI, EJB,
ERP, JMS). Bring in a new buzzword and get an Ontology Works mug. *

A clearance is _not_ required for this position, though it would be nice.

Email resumes and such to jobs@ontologyworks.com. Feel free to include an anecdote from a bug hunt.

This position is open only to US citizens.

* Actually, we don’t have Ontology Works mugs yet.

GetSection vs GetConfig: Solution

Okay, after faffing around for a couple of hours on Google and
experimenting with the new Configuration Management API I managed to
find a solution to my earlier problem. The following code demonstrates
the new way of retrieving the maximum HTTP request size from the
web.config file.

private int MaxRequestLength(HttpContext context)
int result = -1;
// Load the HttpRuntime section from the config file.
Configuration config = WebConfigurationManager.OpenWebConfiguration(“~”);
if (null != config)
HttpRuntimeSection section = config.GetSection(“system.web/httpRuntime”) as HttpRuntimeSection;
// Get the MaxRequestLength property from the section.
if (null != section)
result = section.MaxRequestLength;
return result;

GetSection vs GetConfig

I’m in the process of writing an HttpModule in ASP.NET 2.0 / Whidbey
and have run into a problem when trying to get the maximum HTTP upload
limit. The following code works great in ASP.NET 1.1, but in 2.0 the
call to get the property returns null. I have tried using both
GetConfig (now obsolete in ASP.NET 2.0) and GetSection methods.

private int MaxRequestLength(HttpContext context)
// Load the httpruntime section from the config file.
object section = context.GetConfig(“system.web/httpRuntime”);
Type configSection = section.GetType();
if (null != configSection)
// Use reflection to get the MaxRequestLength property from the section.
PropertyInfo pInfo = configSection.GetProperty(“maxRequestLength”, BindingFlags.Instance | BindingFlags.NonPublic);
if (null != pInfo)
return (int)pInfo.GetValue(section, null);
return -1;

Can anyone help?


I’ve been reading some more of Applied Microsoft .NET Framework Programming this weekend.
This book covers a lot about the .NET Framework that I already know,
but delves deeper into topics, such as what happens at the
CLR level, and how C# translates to IL. I was reading the chapter about
strings on Saturday and decided to post a summary of what the author
(Jeffrey Richter) covers, since not all developers are aware of what
happens to strings under the hood.

Firstly, just to set the record straight, System.String is a
reference type and not a value type, which means each string is
allocated on the heap and not the stack. System.String is peculiar in
that it has a shortcut keyword associated with it
(string), which is a common practice with primitive value
types – System.Int32 (int), System.Char (char) etc. String references
also do not require the new keyword to allocate an instance,
the following line of code will allocate space on the heap, fill it
with string data and assign the reference to the variable:

string aString = “This is a literal string”;

Strings are immutable, meaning that once an instance of 
System.String is created the contents of the object cannot be changed
and is read-only. Immutable strings enable string references to be
copied throughout an application without the need for thread
synchronization.  A large number of string copies in an
application require minimal overhead because only references to
original string objects are copied and not the data itself. 
Immutable strings come with a price, concatenation of multiple string
references using the + operator generates temporary string allocations
for each concatenation operation. The following code will
generate at least 3 temporary strings on the heap before assigning the
result to the result, also a string allocation. Using StringBuilder or
StringWriter are preferred methods over string concatenation.

string a = “a”;
string b = “b”;
string c = “c”;
string d = “d”;
string e = “e”;
string result = a+ b + c + d + e;

The Framework has a neat way of optimizing literal strings in an
application. Each unique literal string reference in an application is
stored in an internal hash table. Whenever a new string allocation is
required the hash table is consulted first to determine if a reference
is already available. Since strings are immutable two references to the
same string object can coexists in the application without causing
conflict. Dynamic created strings (e.g. strings that are the result of
concatenation at runtime) are not added to the internal hash table and
thus require a heap allocation to instantiate them. The Framework
provides a static method called String.Intern, which will add a dynamic string to the hash table if not already interned. The String.IsInterned method behaves the same as String.Intern
accept that it’ll only return a reference to an interned string if it
exists and not add the string to the hash table, otherwise it’ll return
null. Note, once a string is interned it is left in the hash table for
the duration of the default AppDomain, there is no garbage collection
of intern’d strings during the life of the application.

The following code demonstrates string intern:

using System;

namespace StrTest
/// Test string class.
class MainApp
/// The main entry point for the application.
static void Main()
// Strings are immutable (cannot be changed)
string literal1 = “An immutable string”;

// Strings are reference types, following code will return true.
string literal2 = literal1;
Console.WriteLine(“Test1: {0}”,
Object.ReferenceEquals(literal1, literal2));

// The following code will return false (as well as boxing value types).
int intOne = 1, intTwo = intOne;
Console.WriteLine(“Test2: {0}”,
Object.ReferenceEquals(intOne, intTwo));

// Literal strings are interned – shared reference to two identical string.
string anotherLiteral = “An immutable string”;

// The following code will return true.
Console.WriteLine(“Test3: {0}”,
Object.ReferenceEquals(literal1, anotherLiteral));

// Dynamic strings are not interned.
// The following code will return false.
string dynamicString1 = “An”;
string dynamicString2 = dynamicString1 + ” immutable string”;
Console.WriteLine(“Test4: {0}”,
Object.ReferenceEquals(literal1, dynamicString2));

// ReferenceEquals is faster than Equals because Equals tests the value of strings.
// Following code will return true.
Console.WriteLine(“Test5: {0}”,
Object.Equals(literal1, dynamicString2));

// C# compiler will perform one concat operation for concat of literal strings.
// Following code returns true because concat’d literal string is also interned.
string concatStr = “An” + ” ” + “immutable” + ” ” + “string”;
Console.WriteLine(“Test6: {0}”,
Object.ReferenceEquals(literal1, concatStr));

// Following code will return false. Multiple concats should be avoided.
// Following will perform 2 concat operations with at least 2 newly created strings.
string anStr = “An “;
string immutableStr = “immutable “;
string stringStr = “string”;
string badConcat = anStr + immutableStr + stringStr;
Console.WriteLine(“Test7: {0}”,
Object.ReferenceEquals(literal1, badConcat));

// Strings can be interned, following code will return true.
// Intern method will add string to the internal hash table if not already present.
// Note, interned strings remain for the life of the default appDomain (not garbage collected).
string internedStr = String.Intern(dynamicString2);
Console.WriteLine(“Test8: {0}”,
Object.ReferenceEquals(literal1, internedStr));

// It is possible to check for an interned string w/o adding it to the hash table.
// Following code will return null.
string dynamicString3 = dynamicString2 + ” which is not interned”;
Console.WriteLine(“Test9: {0}”, (null ==
String.IsInterned(dynamicString3) ? “null” : “non-null”));

// Following code will return non-null.
string dynamicString4 = “An immutable string”;
Console.WriteLine(“Test10: {0}”, (null ==
String.IsInterned(dynamicString4) ? “null” : “non-null”));

// Further proof that strings are references…
// ChangeStr method passes string reference by reference (reference to reference).
// This means the reference variable parStr can be reassigned to another string
// on the heap. The literal string in the function is a heap object.
string parStr = “original string”;
ChangeStr(ref parStr);
Console.WriteLine(“Test 11: {0}”, parStr);

/// Change the passed string value.
private static void ChangeStr(ref string par)
par = “a new string”;


I’ve just made an update to the version of .TEXT running my blogs to
add code syntax highlighting. Rather than write my own, I incorporated
dp.SyntaxHighlighter from Dream Projections Inc.

dp.SyntaxHighlighter is a Java script solution, so to get it to work I
had to add the right CSS and Java script references in the blog entry
pages. Drop me a line if you would like a list of changes.

With code syntax highlighting, source code in my blog now looks like:

using System;

class DemoClass
// this is a comment
public static void Main(string[] args)
Console.WriteLine(“Hello World.”);

dp.SyntaxHighlighter colorizes all code found in textarea tags, marked with a name attribute containing value code. Since RSS viewers will not render textarea tags I added some code to .TEXT to convert these to p tags when requesting blog pages as XML.

The whole thing works pretty well, except when using the design feature of the FreeTextBox control contained in .TEXT edit pages. Switching from HTML to design mode mangles the source code in the textarea tags. Also, any text following code in a textarea tag is not rendered properly when editing.

Solution: Write the entire post without source code, switch to HTML mode and add the code in textarea tags. Copy the HTML to the clipboard and then post without switching to design mode. If the post doesn’t look right edits can be made by reusing the HTML on the clipboard.

Thread Marshalling

Thought: Wow, haven’t posted much in the software blog recently – been
so busy writing software that I haven’t had time to talk about it.

This week I found out about thread marshalling. Simply put, it involves
marshalling of execution through win-form controls from worker threads.
Justin Rogers writes and excellent article about it here, so I’ll not
go too far into the gory details (or not so gory as it appears).

I wrote a cool thread manager component that maintains a queue of jobs.
The thread manager permits the execution of  a finite number of
worker threads, one per job up to a maximum limit. Once the maximum number of
threads are allocated, and busy, the thread manager will queue up
subsequent jobs until a worker thread becomes free to process the job.

My thread manager component works well, so I built a print manager on
top of it. The print manager manages print jobs (as the name suggests)
and monitors the print spooler for the health of print jobs. When a
print job starts, completes, or is aborted the print manager will fire
an event.

I was talking with a colleague about this the other day and he
mentioned that I need to be careful when calling win-form controls from
my print manager event handlers, since they run on a different
thread to the win-form application. So I looked into this problem a
little further…

Windows maintains a message pump for each windowed
application and pushes messages onto the pump when an event occurs.
This ensures that multiple window events, such as a mouse click or a
keyboard press, do not conflict and the event handlers execute
sequentially. This is all fine and dandy until a worker thread fires an
event, which, when trapped by an event handler, requires some
feedback sent to the user. Fortunately the .NET framework provides a nice
little solution.

Each event handler that responds to events fired by a worker thread
should check to see if the calling thread is the same as that of the
host win-form application (UI thread). The Control class has a method
called InvokeRequired, which will return true if the the current thread
is not the UI thread. Once it’s established that the threads are
different, a call to Control.Invoke will ensure that changes to a
win-form control are made on the UI thread.

Expose a .NET Assembly via COM – without RegASM.exe

I kept promising myself I would write a post on how to expose a .NET
assembly to COM without having to use RegASM.exe. Someone asked me
today for the very code to do this, so I thought now would be as good
as time as any to write my post.

Microsoft provided a wonderful thing when they invented COM Interop.
Most developers know about calling existing COM components from inside
a C# or VB.NET application, but fewer developers know about operating
the system in reverse – calling .NET from VB6 or C++ via COM.

My code, attached below, emulates exactly what RegASM.exe does, but
provides the developer better control over what happens to the registry
and GAC. To successfully expose a .NET class, contained in an assembly, via COM the following things have to happen:

• Said .NET assembly must be added to the GAC (Global Assembly
Cache). There is debate on whether this is true. However, it’s
generally a good idea to install an assembly in a fixed location when
adding registry hooks to that location, and what better place than the
GAC. Non-GAC’d assemblies are liable to break when moved or overwritten
by newer non-backwards compatible versions. The GAC will ensure the
correct version of an assembly is maintained and contained (look, it
even rhymes).

• Registry entries must be added for each exposed class and
interface. Regular COM classes refer to in-proc or out-proc dynamic
link library servers. When exposing a .NET assembly the registry
entries refer to the .NET framework core (mscoree.dll) as the server.
The .NET framework will intercept all COM calls and load the .NET
assembly on behalf of the calling process.

• A type library is to be generated to provide calling applications with a map of exposed interfaces, methods and properties.

The code attached provides all of the above:

Installer.cs – Provides methods to add and remove .NET assemblies to
and from the GAC, and to install and uninstall registry entries.
RegistryValue.cs – Helper class for registry value entries.
TypeLibGen.cs – Helper class for type lib generation.

At the end I have included example code to install and remove
assemblies, as well as what attributes need to be added to a .NE
assembly to make it exportable via COM.

A note on TypeLib generation:

I haven’t been able to find some nice C# code to generate type
libraries from .NET assemblies using reflection, so I had to use the
built in TypeLibConverter method – ConvertAssemblyToTypeLib.
Unfortunately this method doesn’t allow me to specify what classes,
methods and attributes are exported to the type library, it exports all
public classes and types. This can be confusing – the registry may not
contain registry entries for some of these exported types, depending on
how you attribute your code.

To avoid this problem, attribute all public classes and types with
[ComVisible(False)] if not intended to be callable from COM. I show
this in my example at the bottom of the code.

Download code here.

Visio 2003 IFilter and MTA

I recently had a problem instantiating the Visio 2003 IFilter from C#….

The following code instantiates an IFilter using COM Interop. The CLSID
is obtained from the registry (method excluded for brevity) and then used to
obtain the COM object type, the activator then instantiates the object from
the type. I tested the code from a console C# application and it works
great. The CLSID is correctly obtained from the registry for a given
VSD extension and then the COM object is instantiated by the activator.

When I call this same code from a WinForm application the method throws
an invalid cast exception when casting an activated object to an
IFilter. Stepping through with the debugger shows me that the CLSID is
obtained correctly, the type is returned from Type.GetTypeFromCLSID,
and the call to the activator also succeeds.

private static void LoadIFilterFromSource(string fileName, ref IFilter filter)
// Get the extension.
string ext = Path.GetExtension(fileName);
if (null == ext || 0 == ext.Length)
throw new FilterException(“File does not have a known extension!”);

// Get the CLSID of the filter by extension.
string clsID = GetFilterCLSID(ext);
if (null != clsID && clsID.Length > 0)
// Get filter instance.
Type t = Type.GetTypeFromCLSID(new Guid(clsID));
if (null != t)
object filtObj = Activator.CreateInstance(t);
filter = (IFilter) filtObj;
// ^^^^^ Cast Exception thrown here!
catch (Exception ex)
throw new FilterException(“Failed to create IFilter instance!”, ex);

// Did we load the filter?
if (null == filter)
// No, attempt to load via COM.
IUnknown outer = null;
LoadIFilter(fileName, ref outer, ref filter);
catch (COMException ex)
throw new FilterException(“Failed to create IFilter instance!”, ex);

I read somewhere that the threading model for the COM component matters when calling from a multi threaded environment. So, I made sure the calling code to my method is running in a Single-Threaded-Apartment (STA) thread. This change from Multi threaded Apartment (MTA) to STA fixed the same problem with the Adobe PDF IFilter, but did nothing for the Visio IFilter.

I had just about given up when I decided to check the threading model in the registry. I noticed that the PDF IFilter’s threading model is set to Apartment, whereas the Visio threading model is blank. Setting the following registry REG_SZ value to Apartment fixes the problem with Visio also.


I’m not sure if this is an problem with the installation of the Visio IFilter or intended. When I reinstalled the IFilter the threading model was set back to empty. It’s not an elegant solution, but it seems to work.