Archive for the csharp Category

Visual Studio Conditional Breakpoints Pitfalls

Posted in csharp, Visual Studio with tags , on September 8, 2012 by vlipwig

Conditional Breakpoints in Visual Studio are great. They allow you to restrict when the breakpoint will hit based on the evaluation of a Boolean expression. You can choose to hit the breakpoint when the condition is true or when the result of the condition has changed.
But there are a couple of things you should be aware of:

  • that the source language determines the expression operators. This means that if you are debugging in Visual Basic, the “<>” operator means “not equal” while in C# or C++/CLI, you would use “!=”.
  • if you choose a wrong expression operator you get no warning or error. Assume you use “=” (assign) instead of “==” (equal) and you will see that suddenly the variable you are examine will always have the same value.

So do yourself a favor and double check the conditions of your breakpoints.

More infos about conditional Breakpoints:

http://msdn.microsoft.com/en-us/library/7sye83ce.aspx

http://support.microsoft.com/kb/308469

List<T>.BinarySearch Pitfalls

Posted in csharp on July 17, 2010 by vlipwig

The BinarySearch Method of List<T> collection can be very useful to get the zero-based index of an element.

Further more if the element you searched for is not in the List<T> you can calculate the index of the first element that is larger then the element you searched for. Just apply the ~ operator on the result of the BinarySearch:

var pos = aList.BinarySearch(aString);
if (pos < 0)
{
     aList.Insert(~pos, aString);
}

But there is one pitfall: The List<T> must already be sorted; otherwise the BinerySearch result is incorrect.
This is not a big secret. It is clearly stated in the MSDN description for the Method (under Remarks)

I saw a Bug in a Project where someone used a BinarySearch to find duplicated entries between to Lists. This works as long as the Lists are sorted. If not the result is totally nonsense.

It took a while to find this Bug.

MSDN Link:
http://msdn.microsoft.com/en-us/library/w4e7fxsh.aspx

Fun with Extension Methods

Posted in csharp on July 15, 2010 by vlipwig

In a Pair-programming session Fhad showed me some interesting ideas for extension methods.
He and his colleague used extension methods to get rid of those nasty null checking stuff:

if (object != null)
    return object.SomeProperty;
else
    return string.Empty;

They use a generic extension method together with a lambda expression:

public static TResult IfNotNull<T, TResult>(this T obj, Func<T, TResult> func, TResult value) where T : class
{
    return obj == null ? value : func(obj);
}

Here is an example on how to use this extension method:

SomeClass myClass;
string value = myClass.IfNotNull(obj => obj.SomeProperty, string.Empty);
//value contains string.Empty

myClass == new SomeClass();
myClass.SomeProperty = “something”;
value = myClass.IfNotNull(obj => obj.SomeProperty, string.Empty);
//value is now “something”

And here are some more extension methods I’m using:

public static bool HasItems<T>(this IList<T> list)
{
    return list.Count > 0;
}

public static bool IsEmpty<T>(this IList<T> list)
{
    return list.Count == 0;
}

Example on how to use them:

List<string> _stringList = new List<string>;

if (_stringList.HasItems()) ...
if (_stringList.IsEmpty())...

I think this code is much easier to read.

Finally a link to the msdn website about Extension Methods:
http://msdn.microsoft.com/en-us/library/bb383977.aspx
and about Lambda Expressions:
http://msdn.microsoft.com/en-us/library/bb397687.aspx