Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null-conditional operator and string interpolation in C# 6

Tags:

c#

roslyn

c#-6.0

Do the null-conditional operator and interpolated strings syntax resolve to just syntactic sugar?

The null-conditional operator (?.), which allows code clean-up through reducing "excessive" null checking, and interpolated strings (("\{X}, \{Y}")), which brings the arguments and format into one, are new features in C# 6.

Do these get compiled to their undesirable counterparts (i.e. the ugly code we sought to avoid)?

I apologize for the naïve question, I don't have the best understanding of languages in general, but I'm curious if it would be possible to run these features on, say, C# 5.

I know this is the case with Java in some instances, is it true as well with these examples?

like image 208
ChiefTwoPencils Avatar asked Dec 15 '14 21:12

ChiefTwoPencils


People also ask

What happens if you use null value in string interpolation?

If the interpolation expression evaluates to null , an empty string ("", or String. Empty) is used. If the interpolation expression doesn't evaluate to null , typically the ToString method of the result type is called.

What is string interpolation in C?

String interpolation is a technique that enables you to insert expression values into literal strings. It is also known as variable substitution, variable interpolation, or variable expansion. It is a process of evaluating string literals containing one or more placeholders that get replaced by corresponding values.

Can we use ternary operator in string interpolation?

Beyond variablesYou can do that directly with string interpolation, simply by using the ternary operator inside the string: Download, Edit & Run this example!

What is the use of null coalescing operator in C#?

The null-coalescing operator ?? returns the value of its left-hand operand if it isn't null ; otherwise, it evaluates the right-hand operand and returns its result.


1 Answers

There isn't a general rule, it differs. Some features are simply syntactic sugar, some add capabilities that weren't possible before, and some are a combination of both.

Syntactic Sugar

  • String interpolation - This:

    string result = $"{bar}";
    

    Instead of:

    string result = string.Format("{0}", bar);
    
  • Null-propagating operator (?.) - This:

    var result = Foo()?.Length
    

    Instead of:

    var temp = Foo();
    var result = (temp != null) ? temp.Length : null;
    

New Capabilities

  • String interpolation - Also adds support for IFormattable using FormattedString so this is possible:

    IFormattable result =  $"{bar}"
    
  • Await in catch/finally - It's now possible to use await in catch and finally blocks:

    try
    {
    }
    catch
    {
        await Task.Delay(1000);
    }
    

There are of course more features in both categories, like exception filters and expression-bodied members.

like image 87
i3arnon Avatar answered Oct 29 '22 23:10

i3arnon