Simplifying (View Model-) Data Binding Using Expression-Trees

Want to get rid of messy “PropertyChanged”-subscriptions and too many OnPropertyChanged(“…”) to manage value relations? Then DataBinder is what you’re looking for!
Read the rest of this entry »

Casts using “as” (Pitfalls and Best Practices to Prevent Them #5)

Well – “as” is faster to type than a “real” cast using two brackets and has the same result. Really?

C# provides several ways to cast one type to another one. The two used in most cases are the “([Targettype]) [Variable]” and the “[Variable] as [Targettype]” notations. Many developers see them as equivalents, some even prefer “as” because it does not throw an exception when the cast fails.


Let’s say we have a variable called obj of type Object and we want to cast it to an IFoo so we can call IFoo.Bar:

object obj;
(obj as IFoo).Bar();

Read the rest of this entry »

Contracts as a part of the type system? (No Interface Without Contract – Part 7)

Contracts? What are Contracts? And why are they reputed to be concerned with the type system? Aren’t they just injected into the executable? Is there maybe “more” behind them? And how should they really be used? Read this post for clarification.

Looking back

There have been ? parts of the series “No interface Without Contract?” now. It’s not that I told you everything about Microsoft Code Contracts yet and there is nothing more to introduce. In fact this will not be the last part, but there will follow about three.

If you look at the previous parts, you will notice that they are quite distanced: All their content stands – less or more detailed – in the Code Contract’s manual. Thus, you may have noticed that I did not really go “deeper” by talking about my experience using them.

What are Code Contracts?

The reason for that is simple: I did not really think about one question: What are Code Contracts?

Read the rest of this entry »

No Interface without Contract? – Part 6: Object Invariants

Invariants are a kind of postconditions which apply to all members. How to formulate them using Code Contracts?

In the previous part we got started using Microsoft Code Contracts and formulated first pre- and postconditions. We only looked at “simple” ones. In this posting you will learn what object invariants are and how they are connected with Code Contracts.

What are Invariants?

As anticipated in the introduction, invariants can be compared to postconditions valid for all members of a class. In fact, you are actually able to modulate them using Contract.Ensures(invariant) in each member.

However, they are more than simple postconditions – they are always valid, regardless of the object’s state. So invariants must never be broken during an object’s lifetime. Thus, other members can rely on them like on preconditions.

Read the rest of this entry »

Performance Comparisons (a Helper Class)

Not very special but useful: A helper class for performance comparisons using a Stopwatch.

You may know the Stopwatch class which enables much more accurate performance tests than the DateTime struct.

This little helper class makes it use a little bit easier and more comfortable.

Sample Test

Let’s look at a sample test first which introduces the syntax:

static void Main(string[] args)
    PerformanceTestRunner runner = new PerformanceTestRunner(1000, WhileLoop, ForLoop);


    foreach (PerformanceTestResult result in runner.Results)
        Console.WriteLine("Result of {0}: Minimum: {1}; Average: {2}", result.Test.Name, result.MinimumTicks, result.AverageTicks);


Read the rest of this entry »

Distinguish between overloaded methods in XML-Comments

XML-Comments are quite handy when documenting public methods / classes. But what about method overloads in “see”-Tags?

Today when documenting some methods of a class using XML-Comments, I got a ReSharper message saying

Ambiguous reference Bar

However, ReSharper did not provide any automatic solution. So I had to find one myself 🙂

Read the rest of this entry »

Pitfalls and Best Practices to prevent them #4 – Object Finalizers

Let’s help the .NET Garbage Collector! Why this often backfires…

You know that the .NET Framework comes with a Garbage Collector to tidy up unreferenced objects. And you also know that there are so called “Finalizers” (sometimes also called “Destructors”) which are executed before an object’s memory is definitely deallocated.


Some people may think: “Hey – let’s help the Garbage Collector a little bit” and write code like that (Note: The console output is for a later example only):

Read the rest of this entry »