Tag: C#

Create your own extension methods to use in LINQ

The possibility to create extension methods has been around for some time in the .Net Framework. It basically gives you the possibility to extend a class with new methods without having to create a new class that extends the original one. If the original class is sealed you can’t even extend the class, but you

How to prevent duplicate application instances

Sometimes it’s important to make sure your program isn’t running more than one instance at the time. In C# this is an easy task using the provided .NET libraries. In this short illustrative example the program counts the number of processes with the same assembly name as this current assembly. If any are found, the

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

WPF data change notification from custom classes

When binding a WPF control to a custom-made class, changes to property values in the object won’t automatically appear in the control because the control can’t by default recognize changes. The solution is to let the class implement the System.ComponentModel.INotifyPropertyChanged interface. The Interface exposes one event that you execute every time a property changes value.

%d bloggers like this: