Category name:CSharp

Cool finalizer assert trick

I just saw a cool trick done in a finalizer of a class. When a class implements IDisposable then its creator needs to call Dispose when it is finishen. Lots of developers forget this and that usually results in system resources that are locked until the garbage collector thinks its time to do its work.

The code construction I saw was:

~MyCoolClass()
{
    System.Diagnostics.Debug.Assert(false);
    Dispose(false);
}   

I have never thought of doing this but it makes sense to just add a assert to a finalizer to get notified that you didn’t dispose the object. The finalizer will never be called if it would because of the GC.SuppressFinalize(this); statement that should be done when calling IDisposable.Dispose on the object.

It could be that you are getting this in a service and then this doesn’t make any sense but then you could just log an error instead.

Get the physical path of a path that uses a subst drive

My previous employer used a tool to attach databases that were located on a substituted path. I needed this conversion logic in another kind of environment and used the almighty google. So I hit an article on Avner Kashtan‘s blog titled Query SUBST information . His code sample proved very usefull but it didn’t support relative paths on the current drive so I altered it a bit. Below is my version and put it here for other people to use and abuse.

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

public static class Subst
{
public static string GetRealPath(string path)
{
const string MSG_PATH_INVALID = "The path/file specified does not exist.";
const int BUFFER_LENGTH = 1024;

if (path == null) throw new ArgumentNullException("path");
if (Directory.Exists(path))
{
path = new DirectoryInfo(path).FullName;
}
else if (File.Exists(path))
{
path = new FileInfo(path).FullName;
}
else
{
throw new ArgumentException(MSG_PATH_INVALID, "path");
}

string realPath = path;
StringBuilder pathInformation = new StringBuilder(BUFFER_LENGTH);
string driveLetter = Path.GetPathRoot(realPath).Replace(@"", string.Empty);
QueryDosDevice(driveLetter, pathInformation, BUFFER_LENGTH);

// If drive is substed, the result will be in the format of "??C:RealPath".
// after that strip the ?? prefix and combine the paths.
if (pathInformation.ToString().Contains(@"??"))
{
string realRoot = pathInformation.ToString(4, pathInformation.Length-4);
realPath = realRoot + realPath.Substring(2); // Remove the drive letter
}

return realPath;
}

[DllImport("kernel32.dll")]
static extern uint QueryDosDevice(string lpDeviceName, StringBuilder lpTargetPath, int ucchMax);
}

I tried to use Manoli’s csharp to HTML formatter but it screws up. So no syntax coloring this time.

Tool : Add string as resource

I found this really nice little to add a string as a resource. You just select a string in the editor, right click to get the context menu and select to add it as a resource. You then get a dialog in which you can specify the key value and the resourcefile the string should be added too.


In most code I produce I use the string resource generator tool. But this little tool is a very nice addition although I prefer the type-safety that the string resource generator provides.

The && and the & operators

Today a collegue had me flabbergasted by using the & operator in a boolean comparison like the following example.


return x & y;


Both x and y were booleans and I said “Hey you just forgot an &! A single & is a bitwise operator.”. And he responded that this would work but that the behaviour of a single & is that it is not lazy evaluated (short-circuiting).


My knowledge about the csharp language is quite good as is my knowledge of the .net framework so this was quite a surprise!


The funny thing is that the msdn documentation about the & operator doesn’t say anything about lazy evaluation while the documentation about the && operator also mentiones the & operator with boolean operands.

  • Recent Posts
  • Recent Comments
  • Archives
  • Categories
  • Meta