Tip #964: Use latest language features

I often find that developers like comfort zones. They tend to use the constructs that they already know and are comfortable with. That includes language features as well. However, programming languages are evolving all the time and new features are introduced with every new release. Those features aren’t just cool, they make developer’s life much easier in a lot of cases.

One of my favorites is interpolated strings, introduced almost 3 years ago in C# 6.0. Instead of:

Trace.WriteLine(string.Format(
  "Name is {0} {1}", c.firstname, c.lastname));

you can now write:

Trace.WriteLine(
  $"Name is {c.firstname} {c.lastname}");

(Did I also tell you to stop using Console.WriteLine and start using Trace.WriteLine instead? No? That’d be the tip for another day then)

The way I see it, not only this new syntax allows for cleaner and more concise code, it implictly adds value to everything you create by sprinkling your code with dollar signs.

Of course, there are a lot more goodies in version 6.0 than just interpolated strings. There are null-conditional operators, exception filters, index initializers, to name but a few.

And don’t forget C# version 7 either, with out parameters, pattern matching, is-expressions, tuple types and literals, deconstruction, and many others. Like, how cool is this:

(string, string) LookupName(Guid id)
{
  // ... get those names
    return (first, last);
}

Languages evolve and so should your knowledge of them. So, what’s your favorite feature?

2 thoughts on “Tip #964: Use latest language features

  1. John K says:

    Really cool! Looks like you forgot the dollar sign in your sample though.

Leave a Reply

Your email address will not be published. Required fields are marked *