Here is a problem that one of my colleagues who is just starting to use WPF got himself into. He was working on an application that displays items using an ItemsControl and uses a DataTemplate. Inside the DataTemplate he used an Image. Here is the Xaml code:


   1: <Window.Resources>
   2:         <Image Source="Creek.jpg" x:Key="IMG"></Image>
   3:     </Window.Resources>
   4:     <Grid>
   5:         <ItemsControl ItemsSource="{Binding}">
   6:             <ItemsControl.ItemTemplate>
   7:                 <DataTemplate>
   8:                     <Border BorderThickness="2" BorderBrush="Black" 
   9:                             CornerRadius="3" MinHeight="10">
  10:                         <ContentControl Content="{StaticResource IMG}"/>
  11:                     </Border>
  12:                 </DataTemplate>
  13:             </ItemsControl.ItemTemplate>
  14:         </ItemsControl>
  15:     </Grid>

This is a much simpler example, but the principal is the same.


Can you see what was he doing wrong?

Continue Reading...

Here we are talking about filtering list items again :). I got two comment suggesting the use of yield return.

James Curran said:

Don’t build a new list if you don’t have to.

private static IEnumerable EvenSlicker_FilterPositiveItems(IEnumerable t)


foreach(int n in t)

if (n > 0)

yield return n;


The advantage is if you try using the method this this:

foreach(int n in FilterPositiveItems(myList) {…}

Your way is O(2N) while mine is O(N)

Continue Reading...

Hi all


I was into filtering items lately :) and I have decided to write about some of the techniques out there starting with the old


The good old Foreach

Well we have all used this before once you get a list in your hands just iterate through all the items and select the ones you want:

   1: private static List<int> TheOld_FilterPositiveItems(List<int> t)
   2: {
   3:     List<int> ret = new List<int>();
   4:     foreach (int i in t)
   5:     {
   6:         if (i > 0)
   7:         {
   8:             ret.Add(i);
   9:         }
  10:     }
  11:     return ret;
  12: }

I hope you are not using this method. As there are other good methods like this one:


Continue Reading...

todo-list How many times did you write a TODO comment in your code? How many times did you forget about this comment and met it again only some months later? It happens a lot to most of the developers who eventually tends to write their TODO missions on some papers, notepad or a ToDoList application. From what I know, most software developers are not aware of the fact that they can view all of their TODO comments in one list.

Continue Reading...

The thirteen post of the series of programming job interview challenge is out, Only 13 comments with answers were provided to job interview challenge #12. This is a small amount comparing to the previous challenges, but I realize and understand that it was language specific and not very trivial challenge…

Jason Kikel was the first one to solve the question, and here is his short answer:

UnmanagedClass is referencing an address on ManagedClass without pinning it. The ManagedClass instance needs to be pinned so the GC won’t move it to another location during a collection.

Continue Reading...

Here is something neat I found out.

Say you are writing an application and one of the requirements is to allow File System search. You could always start using loops and such. I thought to myself why not do it in LINQ? I played around with it and in fact it is not so hard.

Lets see how it is done. Here is method that allows finding a specific file name in side a directory.

   1: private List SearchFilesByName(string DirectoryPath, string FileName) 
   2: { 
   3:     return (from file in new DirectoryInfo(DirectoryPath).GetFiles() 
   4:             where file.Name == FileName select file).ToList(); 
   5: }

Basically we Query the FileInfo[] which is returned from the GetFiles() method and compare the file name.

Continue Reading...

The twelfth post of the series of programming job interview challenge is out, 28 readers provided answers to job interview challenge #11. I have to admit that I probably failed explaining what I was looking for in challenge #11, because I asked you to provide the best algorithm in both manners: performance and memory. What I really meant is that performance is most important but don’t neglect the memory issue. Due to my little “embarrassing failure”, there are two groups of correct answers – the performance oriented and the memory oriented.

The correct answer which I was looking for (best at performance) as Alex, the first one to provide a detailed solution (its two times in a row), wrote:

Continue Reading...


In my previous article I talked about why SortedList is not a good option to use if you need a sorted collection with keys that are not unique. Today I will show you how to use a regular generic List<T> to store sorted items. You basically have 2 options.

IComparable Interface

this means that you will have to make your stored class Implement the IComparable Interface. Here is an example:

Continue Reading...

The eleventh post of the series of programming job interview challenge is out. 75 readers provided answers to job interview challenge #10 and most of them had the correct solution. The correct answer as Alex, the first one to provide a detailed solution, wrote:

Here’s an O(N) solution:

Have one variable that stores the sum of all the values. Iterate through the list to add all numbers to the variable, which for simplicity I will call “listSum”.

Now, take the theoretical sum of all numbers between 1…n+1: This can be computed in O(1), as the sum of numbers up to N is n(n+1)/2: So for numbers up to n+1, it’d be (n+1)(n+2)/2 (for instance, if the array is of size 9, we’d do 10*(11)/2, or a theoretical sum of 55). Call this sum “allSum”.

The missing number will be the result of computing “allSum – listSum”.

O(N)complexity, with only two tracking variables.

Continue Reading...

Did you ever need to convert List(T1) to List(T2)? One example might be when implementing an interface. you might need to expose a collection of other interfaces (or maybe the same interface), But you usually hold the concrete type implementing the interface in the collection. Lets look at the following example:

Continue Reading...