Monthly archives: June, 2013

Do you know what your C# variables contain?

It might sound like a trivial question but there are several ways to analyse a variable and they might give different answers. It’s therefore good to know what the difference in-between them are. The different tools we’ll go through here are: is, typeof and GetType. To include the complexity of inherited objects we create two




Missing the timer in WPF?

It’s not gone, it has just moved away from being a UI component to become a code class. In WPF it’s called Dispatcher Timer and can be used in the following way: using System.Windows.Threading; public MainWindow() { InitializeComponent(); DispatcherTimer dispatcherTimer = new DispatcherTimer(); // Register method target to execute every tick dispatcherTimer.Tick += DispatcherTimerTick; //




Have you tried the null coalescing operator in C#??

It’s quite handy and can make the code a bit more lean if you need to do a lot of null value testing. Here is a simple example: processCar = inputCar ?? new Car(); What it basically mean is “if inputCar is null then create a new car instead”. The code can be unfolded in




Add a notification icon to your WPF program

I needed to create a WPF program that will run at certain intervals and, when running, always will do it in the background. Not that difficult. But I also wanted it to be accessible through a notification icon down in the right corner on the Windows screen. A little more work, but not that bad.




Complex syntactical sugar using yield in C#

Yield is one of the available contextual keywords in C#, meaning it has a purpose in the code but it’s not a reserved word. Microsoft defines yield as being: Used in an iterator block to return a value to the enumerator object or to signal the end of iteration The keyword was added in .NET




Implement a complex type in MVC4 and EF

When you create models in Entity Framework you have the possibility to generalize parts of it into reusable types by adding the ComplexType attribute. To illustrate I have a made up example of an access definition type that can be used on many different user-to-something connections to define what kind of access the user has.




How to bind a WPF ListView to a list

As with many controls in .Net, binding is a powerful way to display data from a data source. In this simple example we’ll show you how to bind a ListView to a list of objects. We’ll start by defining the data we want in the list by creating a class with properties. In this simple




%d bloggers like this: