7

Today, I pull the code from my client, and I get an error in this line.

throw new Exception($"One or more errors occurred during removal of the company:{Environment.NewLine}{Environment.NewLine}{exc.Message}");

This line also

moreCompanies = $"{moreCompanies},{databaseName}";

The $ symbols is so weird with me. This is C# code.

Martin Smith
  • 438,706
  • 87
  • 741
  • 845
user3682707
  • 267
  • 1
  • 13
  • It's a little unfair to downvote this with a brand new feature that's only just been released – Immortal Blue Jul 31 '15 at 07:47
  • 3
    @ImmortalBlue I also feel like it is hard to Google for symbols like `=>` and `$`. – Alex Booker Jul 31 '15 at 07:58
  • 1
    @ImmortalBlue, it's a zero-effort question. That's what down-votes are for. Google would have answered this question in less time I spend to write this comment. – Patrik Jul 31 '15 at 08:34
  • Well, I spent five minutes googling c# string and dollar sign and every variant I could think of, and all the results that came up were for how to escape the character or other usages. – Immortal Blue Jul 31 '15 at 09:14
  • @ImmortalBlue you can also try the documentation itself. You won't be able to google for a *lot* of C# operators like ?, ??, ?: – Panagiotis Kanavos Jul 31 '15 at 09:21
  • 2
    @PanagiotisKanavos You mean like this fantastically upvoted almost identical question? http://stackoverflow.com/questions/446835/what-do-two-question-marks-together-mean-in-c – Immortal Blue Jul 31 '15 at 10:21
  • 1
    @ImmortalBlue or [this](http://stackoverflow.com/questions/31764532/what-is-the-assignment-in-c-sharp-in-a-property-signature) that appeared over the weekend. Agree and upvote – Panagiotis Kanavos Aug 03 '15 at 07:38
  • 1
    I also think the client should have given warning that they are switching compilers – Panagiotis Kanavos Aug 03 '15 at 07:41

2 Answers2

11

The $ part tells the compiler that you want an interpolated string.

Interpolated strings are one of the new features of C# 6.0. They allow you to substitute placeholders in a string literal with their corresponding values.

You can put almost any expression between a pair of braces ({}) inside an interpolated string and that expression will be substituted with the ToString representation of that expression's result.

When the compiler encounters an interpolated string, it immediately converts it into a call to the String.Format function. It is because of this that your first listing is essentially the same as writing:

throw new Exception(string.Format(
    "One or more errors occured during removal of the company:{0}{1}{2}", 
    Envrionment.NewLine, 
    Environment.NewLine, 
    exc.Message));

As you can see, interpolated strings allow you to express the same thing in a much more succinct manner and in a way that is easier to get correct.

Alex Booker
  • 10,487
  • 1
  • 24
  • 34
  • Thank you very much ! You saved my time :) – user3682707 Jul 31 '15 at 07:35
  • 1
    It is worth mentioning that if you get an error on that line (as you say you are), then you probably don't have Visual Studio 2015. You need to either 1) upgrade to the new Visual Studio 2015, or 2) change all that code back and possibly, 3) talk to your client about not using those features until you've upgraded. – Lasse V. Karlsen Jul 31 '15 at 07:53
  • @LasseV.Karlsen Thank you for this addition. – Alex Booker Jul 31 '15 at 07:58
2

This is the new string interpolation introduced in C# 6

György Kőszeg
  • 17,093
  • 6
  • 37
  • 65