Monday, February 4, 2008

chaining the C# ?? Operator

?? operator was news to me, Rick Stahl's blog is great: http://www.west-wind.com/weblog/posts/236298.aspx. snippet:

string value1 = null;
string value2 = "Test1";
string result = value1 != null ? value1 : value2;

which causes result containing Test1 or the second value.

In C# you can shortcut this special null comparison case with the new ??:

string result = value1 ?? value2;

Friday, January 25, 2008

debug .net library code

msft released the code to a whole bunch of .net libraries last week, Shawn Burke details the steps to take to see it:

http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx

Friday, December 21, 2007

helpful sql perf queries

Ian Strike has a very interesting article on some of the perf stats that sql2005 automatically maintains:

http://msdn.microsoft.com/msdnmag/issues/08/01/SqlDmvs/default.aspx

Thursday, November 1, 2007

sourcevault DOES do unicode diffs

not sure how i missed this, but in the Diff window, go to Tools…Options… then in ‘Character Encodings’ section, pick UNICODE. when you refresh the diff, it’s happy--

Friday, June 22, 2007

avoid finalizers/use "using"

this is something we’ve gotten wrong a bit in places: in c#, only use a finalizer (i.e. a ~ClassName() method) if you need to clean-up unmanaged resources you’ve allocated. the reason for this is that if your class has a finalizer, the garbage collector will place it in the finalization queue and will not release it immediately. if you do have unmanaged resources and require a finalizer, implement IDisposable and do your cleanup in a shared Dispose(bool isDisposing) method. make sure to call GC.SuppressFinalize() in your Dispose() method so that if your client has already called Dispose(), your object is not placed in the finalization queue. for example:

public class ClassName : IDisposable
{

private bool m_IsDisposed;

~ClassName()
{

Dispose(false);

}

public void Dispose()
{

Dispose(true);

GC.SuppressFinalize(this);

}

public void Dispose(bool FromDisposeMethod)
{

if (!m_IsDisposed)
{

m_IsDisposed = true;

if (FromDisposeMethod)
{

//release managed resources; only do this if from Dispose method since they themselves might have been finalized

//...

}

//release unmanaged resources

//...

}

}

}

also, use "using" as much as possible. you can do this with any object that implements IDisposable:

using (MemoryStream ms = new MemoryStream())
{

//do whatever you want with ms

//...

}

using will automatically call Dispose() when the scope of this block ends, so you get very readable code and know that resources are properly released--

Monday, June 11, 2007

beware BIT columns in SQL2000

found a strange one today… if you have a BIT column and you write the following TSQL:

WHERE BitColumnName = 1

SQL Server doesn't quite do what you want it to do. rather than converting the 1 to a bit and then applying the where clause filter, it instead converts BitColumnName to an int and then applies the filter. this is problematic since you won’t be properly using indexes you have on the bit column. to avoid this, write:

WHERE BitColumnName = convert(bit, 1)

this is fixed in SQL2005--