Technical solutions and general life savers

C# 7.0

Sometimes our lives as programmers get a bit easier, Microsoft has launchedlaunched C# 7.0 some time ago. Here are a few examples of the things that made our lives a lot easier.

The microsoft page has better explanations, the code below is just random code from my project. These examples are only intended to outline some of the new features.

Pattern matching

1

2

3

4

5

if(sender isToolStripMenuItemTranslate)

{

ToolStripMenuItemTranslate item=(ToolStripMenuItemTranslate)sender;

TabPageAddOrShow(item.CustomTabPage,ModifierKeys==Keys.Shift);

}

Can be rewriten as:

1

2

3

4

if(sender isToolStripMenuItemTranslate item)

{

TabPageAddOrShow(item.CustomTabPage,ModifierKeys==Keys.Shift);

}

Using the underscore we can ignore variables, which keeps the compiler (and resharper) happy: DoSomething(out String variable1, out String _)
Which brings us the pleasure of inline variable definition as well as ignoring unused ones.

It can also create funny code (introducing underscores in useless places):

1

if(sender isToolStripMenuItemTranslate_)

Tuples
While we have had basic tuples for a long time they grew up and gave us a weapon against the useless class/structs to perform returns (or way to many out variables).

1

public(Stringresults,Int32 exitCode)ExecuteCommand(Stringcommand)

Keep in mind that a tuple is a struct!

Switch statements with patterns
While switching still isn’t available on primitive types it sure improves our lives a lot: