DEV Community

Cover image for C# - Handy Features
Dalbir Singh
Dalbir Singh

Posted on

4 2

C# - Handy Features

C# has been improved greatly over the years with useful features that have increased productivity for developers.

Below I’ll explain a few of my favorite features.

String Interpolation

In many programming languages, writing a string with embedded variables can be cumbersome. The longer the string, the more noise is added affecting readability like so:

Alt Text

C# 6.0 introduced a new string interpolation feature in which the variables can be written as expressions without the need for awkward string concatenation:

Alt Text

This results in the same output, but the code is much easier to read and maintain. To use this feature the string must begin with the $ symbol . String formatters may also be applied as shown in the Score variable.

As a plus – expressions will be highlighted within the whole string for better visibility in Visual Studio.

Expression-bodied function members

If you’ve created a few C# POCO classes over the years, I’m willing to bet many of the properties you’ve written are single read only statements.

Here’s a typical example where FullName is a read only property:

Alt Text

...And now here’s the same example using the FullName property as an expression-bodied function member:

Alt Text

The output is identical to the previous example. The code has been compacted down to one line, reducing noise.

Null-conditional operators

Null checking is an important subject for producing stable apps. Until now, null checking has often been verbose. Here’s a typical null checking statement combined with a ternary operator:

Alt Text

With C# 6.0 null checking becomes more succinct:

Alt Text

In this example the variable firstName is assigned null if the person object is null. If the person object is not null, the value of the FirstName property will be applied. This works thanks to the ? operator.

But what if we want to supply a substituted value for null values, similar to the ternary operator approach?

This can be solved by using the null coalescing operator:

Alt Text

Here, the expression always returns a string. The rules of the ? operator ensure that the left-hand side of the operator is evaluated only once.

Happy Coding 🤓

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed: Zero in on just the tests that failed in your previous run
  • 2:34 --only-changed: Test only the spec files you've modified in git
  • 4:27 --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • 5:15 --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • 5:51 --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video 📹️

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay