The New C# 6.0 Language Features Explained

I am a professional and passionate software developer and have been using the Microsoft :NET platform for more than a decade now.

A few months ago the new C# compiler was released which supports the new language version C# 6.0.

The following video which I recorded “wiff ze German English accent” I explain some of the new syntax features.

Below the video on Youtube – but here as well – you’ll find the time indices at which a new feature begins:


Read more

Yield return & using IDisposable

Deutsche Version

“Yield return” is a powerful and handy statement if you want to quickly and easily an iteratable list without creating an Array or a List first:

using System;
using System.Collections.Generic;
using System.Drawing;

class Program
{
    static void Main()
    {
        var colors = Rainbow;

        Console.WriteLine("colors.GetType(): {0}", colors.GetType());
        Console.WriteLine();

        foreach (Color color in colors)
        {
            Console.WriteLine(color.Name);
        }

        Console.ReadLine();
    }

    static IEnumerable<Color> Rainbow
    {
        get
        {
            yield return (Color.Red);
            yield return (Color.Orange);
            yield return (Color.Yellow);
            yield return (Color.Green);
            yield return (Color.LightBlue);
            yield return (Color.Indigo);
            yield return (Color.Violet);
        }
    }
}
Red
Orange
Yellow
Green
LightBlue
Indigo
Violet

The .NET compiler then creates the necessary IEnumerable- and IEnumerator-implementing classes and the state machine in the background and everything is fine and clear.
Read more

Linq: Split()

Deutsche Version

Terminator T1000

So you know the Cinderella saying “The good ones go into the pot, the bad ones go into your crop.”?

Well, .NET Linq does have a solution for either one, it’s called Where(). If you use that, your solution probably looks like this:

var evens = list.Where(number => ((number % 2) == 0));
var odds =  list.Where(number => ((number % 2) != 0));

But it does not have a solution for both. So I wrote myself one:

public static void Split<T>(this IEnumerable<T> source
    , Func<T, Boolean> predicate
    , out IEnumerable<T> trueList
    , out IEnumerable<T> falseList)
{
    trueList = source.Where(predicate);
    falseList = source.Except(trueList);
}

Read more

WPF: MVVM & Responsive UI

Deutsche Version

A few days ago I wrote about my first tumbling steps into the world of WPF & MVVM.

Now a friend of mine – who’s been working in that very field – has challlenged me:

a) My GUI should remain responsive while the merge is being executed.

b) I should not re-evaluate the entire GUi after every single user action.

Read more

My GetService Pattern

In .Net the Interface System.IServiceProvider is a simple and elegant method to query services from other objects without having – or even wanting – to know how the class structure behind it looks like. Is the interface at that class implemented or at another? No matter, you query IServiceProvider and you get an instance of what you asked for.

That’s the theory. In reality the developer may have forgotten to add the new interface to the list after he implemented it. Or he forgot the IServiceProvider altogether.

That’s why I have this method to get what I want through several fallback strategies:

Read more

My XmlSerializer Pattern

The idea behind the .NET System.Xml.Serialization.XmlSerializer class is that you can easily serialize an XML file into a class tree and vice versa.

If you’re using XML Schema files, the xsd.exe tool can create that class tree for you.

But for simple applications you don’t even need a schema. You simply create a class that has public properties of simple types or complex types that in turn contain simple types.

Then you can throw this object into the XmlSerializer and you’re done. 🙂

Here’s my personal XmlSerializer coding pattern for a simple list of settings:

Read more

Windows 8 – The Keyboard Shortcuts You Should Know

Windows 8 was written primarily for touch devices such as tablets and touch screen monitors.

If you’re a classic mouse and keyboard user there will be some challenges.

For example, did you know that if you want to close one of the new Metro apps, you have to push your mouse to the top of the screen until it turns into a hand and then you have to drag the app all the way down to the bottom of the screen until it disappears?

Also, the new Windows 8 is designed to have a special function in every corner of the screen. To open the Start screen you have to move your mouse into the lower left corner until a small Start screen popup appears and then click.

But what if you have two monitors and the right one is your primary monitor just like in my case? Try to quickly hit the small area on the bottom left of the right screen when the mouse cursor has a whole other screen to wander into…

That’s why I’m glad that I grew up during DR DOS and MS-DOS and still know how to use keyboard commands and shortcuts.

For example, I have not opened an MS Office program via icon in years. Not in Windows XP, not in Windows 7 and I won’t do it in Windows 8. You open the Run window via Windows key plus R (Win+R) and type “winword” or “excel” or “powerpnt” and off you go… For the Calculator “calc”, the Character Map “charmap”, the Paint program (e.g. for quick screenshots) “mspaint”, …

Luckily for me, Windows 8 brings some new shortcuts with it which can be used to open the most important functions. The keys used are not always intuitive but my guess is that the alphabet has only so many letters and many are already in use.

But nevertheless, why the function “Find Apps” has the shortcut Win+Q instead of Win+A and “Find Settings” has Win+W instead of Win+S I do not know.

So, here’s a list of shortcuts new and old that might come in handy for the usage of Windows 8:

Windows key (Win) – Formerly opened the Start menu, now opens the Start screen (no need to find the lower left corner of the screen). Press Win again and the Start screen closes.

Before Windows 8 I found this to be pointless unless you were unlucky enough to have a computer without a mouse. Now it actually becomes useful.

Win+Q – Opens the search for applications in the former start menu.

Win+W – Opens the search for settings.

Win+F – Opens the search for files.

Win+E – Opens a new instance of Windows Explorer.

Win+R – Opens the Run window.

Win+I – Opens the Settings sidebar.

Win+P – Opens the multi-screen options (e.g. to use a projector in a meeting).

Win+D – Shows the Desktop. Another press of Win+D returns all programs to their previous state (important difference to Win+M).

Win+L – Lock computer.

Win+X – Show a list of shortcuts to various system functions.

Win+C – Show the right side bar (no need to find the lower/upper right corner of the screen).

Win+M – Minimize all windows and thus show Desktop.

Read more