Cool .NET Tips and Tricks #18
By Dr. Dexter Dotnetsky
There is no "Printer - Friendly Version"
Dr. Dotnetsky

Yup, its me again. This rant has a bunch of little tidbits all accumulated since my last "UnArticle", so rather than try to categorize them, I'll just rant on and y'all can try to sort out the goodness yourself.

Cool Stuff I've Found Recently

Windows XP SP2, engineering marvel that it is, already has some exploits and hotfixes; among them are the fix for the loopback dealie that SP2 cripples certain types of software.

GhostDoc is the Add-In contest winner - its a neat VS.NET add-in that reflects on your methods and helps by filling in the C# XML Documentation comment block for you.

FirstFloor software has their FirstFloor.Msn API for MSN Messenger that uses the MSNP 9 protocol and supports file transfer, email and all that other cool IM stuff. The free developer download includes a full-featured demo app that will give you all the code you need to start doing Messenger stuff programmatically. It has a 30 minute maximum connection time limit, which is really no big deal.

Microsoft picked up Lookout Software recently and now makes available the Lookout Outlook Add-in . Now this is very cool, in Dr. Dotnetsky's humble opinion. I can have this index not only all my Outlook folders, but even any folders anywhere on my machine and search for stuff and it brings up the links right in MS Outlook. And, brother - this thang is FAST! If you're anything like Old Dr. Dotnetsky, who is beginning to have more and more Senior Moments and often can't even remember where he left something he was working on ten minutes ago, you gonna like this puppy!

ClearType Tune-Up, Anyone?

Microsoft developed ClearType technology specifically for LCD screens. Here's a page that lets you actually "Tune" your ClearType settings online, along with other very interesting stuff about how ClearType works.

Cool Email SpamBot Link Code

There are plenty of neat encryption / encoding schemes to show an email link in a page but prevent it from being harvestable by spam bots, but here is one that is elegantly simple:

<script language=javascript>
function makeEmailLink(username, domain, linktext)
document.write("<a href=" + "mail" + "to:" + username + "@" + domain + ">" + linktext + "</a>");

Hey, if I have to tell you how to use the above, please go back and study for your first MCSE exam or something!

Myths about .Net and C# best-practices Department

This stuff is selected from a series of blog posts and comments thereon published by somebody possessing an above-room temperature IQ (actually I'm noticing that selective blog-reading can help make you regular; I've even downloaded a copy of RSSBandit and i like it). The ones I've selected are interesting:


Always set your objects to null (or Nothing) when you're done with them.


The garbage collector won't be able to free an object if any another object is referencing it.


The garbage collector can free any object that is unreachable from any of the various heap roots. If object A references object B, and object A is unreachable, object B can be released if it has no other references from reachable objects.
However- it appears to matter although in very few cases --
(see here: )


StringBuilder.Append() is always faster than String.Concat().


String operations always create new string objects, whereas StringBuilder uses internal magic to avoid expensive allocations.


Using a StringBuilder is sometimes (even usually) faster than using simple string concatenation, but there is a performance cutover point. Various people have found this to be around the 5 - 10 concatenations mark. The bottom line being that replacing this:

string fullName = firstName + " " + lastName;

with this:

StringBuilder builder = new StringBuilder();


builder.Append(" ");


string fullName = builder.ToString();

is rather pointless. Remember that StringBuilder is itself reference type, and has the associated allocation / deallocation costs.


Strings are passed to, and returned from, methods by-value.


Strings are immutable, therefore they must have value type semantics, therefore must be copied by value.


Strings have certain value-type semantics (namely, immutability), but are otherwise heap-based reference types, and references to them can indeed be passed or copied. Passing a string reference to or from a method does not copy the underlying string.


Dispose() releases an object's memory.


Well, it's called 'Dispose' isn't it?


Dispose() is merely a convenient place for a class' developer to place any resource clean-up code, in the hope that clients will call it. Dispose does not free the managed memory allocated for an object. The only thing that can do that is the garbage collector; there is no way to deterministically (or manually) release memory. Dispose() does have another benefit. If you implement IDisposable, you can write something like:

using (Blah b = new Blah()) {

At the end of that block, compiler will put the code to call Dispose automatically. Useful when you acquire expensive resources.


Keeping ADO.Net connection objects open for the duration of a [Page/Control/1000-line method]'s lifetime is more efficient than repeatedly opening and closing it around each operation.


Connecting to a database is expensive, so try to avoid doing it too much.


If your managed provider allows connection pooling, and if pooling is enabled (it is, by default, for the managed SQL Server provider at least), calling Close() on the connection object does not simply close the underlying connection.
It merely releases that connection back to the pool where it can be associated with another managed connection object at some time in the future. This is a Good Thing, especially in multi-threaded environments such as ASP.Net,
because physical connections are rare resources and should not be coveted. (By the same token, calling Open() on a managed connection when a pooled physical connection is available will simply associate the former with the latter).

By keeping a managed connection in an open state, you are preventing objects in other threads from getting hold of them, leading to potential bottlenecks.


Int32 and int are not the same thing (float/Short, long/Int64, string/String, etc.).


They're spelled differently.


The C# simple type keywords are interchangeable with the equivalent BCL types; they are aliases.

Political BS Department

I'm Dr. Dexter Dotnetsky, and I approve this message.

I've already made up my mind. They say that you "become a conservative once you have something to conserve". We are in a war against maniacs who have no regard for Healthcare, jobs, or Social Security. Their goal is to kill Americans and anybody who supports America, inflicting as much damage as possible. You can talk all you want about a kinder, gentler war on terrorism. Go ahead and hold your breath waiting for the French and the Germans to help us out. If you are a software developer, think about what's gonna happen to your job if we don't get rid of these monsters just as fast as we can, whatever it takes. It's unfortunate, but that's the platform at this particular point in time. Dr. Dotnetsky is saddened by how many otherwise intelligent Americans have simply not learned the lessons of history.

I was on the alt newsgroups recently and one guy had posted pirated PDF / CHM copies of about 100 different developer books. They all had the subject line "BUSH IS OUR NATION'S GREATEST THREAT TO DEMOCRACY" - followed by the title of the book. You know what? - Doesn't this just say it all! Most of these people are just too dumb to "get it". A few of them are both stupid and downright dangerous. On the other hand, you've got people like Michelle Malkin that really need to be slapped, too.

Classes vs. Objects Department

There is a lot of syntactical confusion in the .NET space, particularly among former C++ and JAVA developers, about what's the difference between a Class and an Object. Let me put this to rest very simply with Dr. Dotnetsky's Immutable Law of Objects:

An Object is an instance of a Class

The qualifying word here is "instance" - that's all there is to it. Feel better now?

Well, that's all I"ve got for now, and besides my Martini needs to get refreshed.



Dr. Dexter Dotnetsky is the alter-ego of the forums, where he often pitches in to help answer particularly difficult questions and make snide comments. Dr. Dotnetsky holds no certifications, and does not have a resume. Always the consummate gentleman, Dr. Dotnetsky can be reached at  Dr. Dotnetsky's motto: "If we were all meant to get along, there would be no people who wait for all the groceries to be rung up before starting to look for their damn checkbook."