Clean event handler invocation with C# 6

Jon Skeet's coding blog

The problem

Invoking event handlers in C# has always been a bit of a pain, because an event with no subscribers is usually represented as a null reference. This leads to code like this:

It’s important to use the handler local variable, as if instead you access the field twice, it’s possible that the last subscriber will unsubscribe between the check and the invocation:

Now this can be simplified slightly using an extension method:

Then in each event, you can write a single line:

However, this means having a different extension method for each delegate type. It’s not too bad if you’re using EventHandler but it’s still not ideal.

C# 6 to the rescue!

The null-conditional operator (?.) in C# 6 isn’t just for properties. It can also be used for method calls. The compiler does the right thing (evaluating the expression only once) so you can do…

View original post 407 more words

C#6 – String interpolations

String interpolation lets you more easily format strings. String.Format and its cousins are very versatile, but their use is somewhat clunky and error prone. Particularly unfortunate is the use of numbered placeholders like {0} in the format string, which must line up with separately supplied arguments:

var s = String.Format("{0} is {1} year{{s}} old", p.Name, p.Age);

String interpolation lets you put the expressions right in their place, by having “holes” directly in the string literal:

var s = "\{p.Name} is \{p.Age} year{s} old";

Just as with String.Format, optional alignment and format specifiers can be given:

var s = "\{p.Name,20} is \{p.Age:D3} year{s} old";

The contents of the holes can be pretty much any expression, including even other strings:

var s = "\{p.Name} is \{p.Age} year\{(p.Age == 1 ? "" : "s")} old";

Notice that the conditional expression is parenthesized, so that the ‘: “s”’ doesn’t get confused with a format specifier.

Note: This describes the syntax that works in the Preview. However, they have decided to change the syntax, to even better match that of format strings. In a later release you’ll see interpolated strings written like this:

var s = $"{p.Name,20} is {p.Age:D3} year{{s}} old";

They are adding a scheme so that you can influence the formatting behavior; e.g. to use Invariant Culture.

Looking ahead, and looking back

I’m a bit late this year, but here are my thoughts on the Software industry as we move into 2015.

I’ve said it before: The talent war is over, and the talent won. Software developers are in high demand everywhere. My readers probably know this. You likely get as many recruiter emails as I do every week. I don’t see this changing. All the demographic and economic information available indicates that demand for software developers will continue to outpace the supply of people with those necessary skills.

But don’t be complacent

But, like all shortages, economics will change this situation as well. More and more people are entering the software field because there is such high demand for developers.But, unlike a generation ago, you will need to compete against people everywhere in the world. If you want to stay in demand, you need to have more skills besides core software development.

There are many directions to go here in addition to the traditional advice of learning the business and adding soft skills. Are you good at explaining software development to others? Help mentor teams. Do you have some of the specific skills that are in high-demand? (Read on for my thoughts on what those might be.) Are there particular verticals you want to explore?

Whatever it is, become a multi-dimensional asset. One day, “folks that can code” will not be as in demand as they are now. But, high-quality software people will still be in demand.

Continue reading “Looking ahead, and looking back”

Whats new in C# 6.0


  • Exception Filters : Exception filters are a CLR capability that is exposed in Visual Basic and F#, but hasn’t been in C# – until now. If the parenthesized expression after ‘if’ evaluates to true, the catch block is run, otherwise the exception keeps going. Basically Exception Filter is one of the new features of C# 6.0 that allows us to specify a conditional clause for each catch block. In other words now we can write a catch block that will handle the exception of a specific type only when a certain condition is true that is written in an exception filter clause. First we see a code snippet of an Exception Filter then we will learn more about it.
        static void Main(string[] args)
                throw new ArgumentNullException("Arg1");
                //throw new ArgumentNullException("Arg2");
            catch (ArgumentNullException ex) if (ex.Message.Contains("Arg1"

View original post 1,359 more words

How C# 6.0 Simplifies, Clarifies and Condenses Your Code

C# 6.0 isn’t a radical revolution in C# programming. Unlike the introduction of generics in C# 2.0, C# 3.0 and its groundbreaking way to program collections with LlNQ, or the simplification of asynchronous programming patterns in C# 5.0, C# 6.0 isn’t going to transform development. That said, C# 6.0 will change the way you write C# code in specific scenarios, due to features that are so much more efficient you’ll likely forget there was another way to code them. It introduces new syntax shortcuts, reduces the amount of ceremony on occasion, and ultimately makes writing C# code leaner. In this article I’m going to delve into the details of the new C# 6.0 feature set that make all this possible. Specifically, I’ll focus on the items outlined in the Mind Map shown in Figure 1.


Continue reading

#1,212 – List of Features Shipping in C# 6.0

2,000 Things You Should Know About C#

Thanks to Steve Hall for pointing out the location of the following information.

You can get the current status of all language features that will be present in the upcoming C# 6.0 release, as part of the Roslyn compiler platform project.  Here’s the link to the page on Codeplex containing a current list of features:

Language feature implementation status for Dev14

For reference, below is the list of planned C# 6.0 language features, as of 24 Oct, 2014.

Features that are “done” (already implemented):

  • Auto-property initializers
  • Getter-only auto-properties
  • Constructor assignment to getter-only auto-properties
  • Parameterless struct constructors
  • Using static members
  • Dictionary initializer
  • Await in catch/finally
  • Exception filters
  • Expression-bodied members
  • Null propagation
  • nameof operator
  • #pragma

Features that are “planned” (intended for the release):

  • String interpolation

View original post

C# 6 in action

Jon Skeet's coding blog

Now that the Visual Studio 2015 Preview is available and the C# 6 feature set is a bit more stable, I figured it was time to start updating the Noda Time 2.0 source code to C# 6. The target framework is still .NET 3.5 (although that might change; I gather very few developers are actually going to be hampered by a change to target 4.0 if that would make things easier) but we can still take advantage of all the goodies C# 6 has in store.

I’ve checked all the changes into a dedicated branch which will only contain changes relevant to C# 6 (although a couple of tiny other changes have snuck in). When I’ve got round to updating my continuous integration server, I’ll merge onto the default branch, but I’m in no rush. (I’ll need to work out what to do about Mono at that point, too –…

View original post 1,520 more words

C# 6.0 – Using Static

Didactic Code

In this installment of my ongoing series covering likely C# 6 language features I’ll be covering one of the features listed as “Done” on the Language feature implementation status page: using static. The idea behind using static is to allow importing members from static classes thus removing the requirement to qualify every member of an imported class with its owner. For the F#ers reading this, using static brings open module to C#.

Consider a method that writes some text to the console:

In this example, we’ve had to specify the Console class three times. The using static feature we can simply invoke the WriteLine method:

The primary benefit in this contrived example is eliminating redundancy but consider a more practical example which makes use of some of System.Math’s members:

Here we’ve had to qualify both PI and Pow with the Math class. Granted, it only appears twice when calculating the area…

View original post 86 more words

Blog at

Up ↑