Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using string interpolation and nameof in .VS 2015 NET 4.5

I'm using things like $"hello {person}" and nameof(arg1) in my code, but on checking the project properties I'm targeting .NET 4.5.

Is this okay? I thought these things were introduced in 4.6.

The project builds and runs okay on my machine - but I'm worried something will go wrong when I deploy it.

like image 301
PeteGO Avatar asked Jan 18 '16 16:01

PeteGO


People also ask

How do you interpolate a string in C#?

An interpolated verbatim string starts with the $ character followed by the @ character. Starting with C# 8.0, you can use the $ and @ tokens in any order: both $@"..." and @$"..." are valid interpolated verbatim strings. To include a brace, "{" or "}", in a result string, use two braces, "{{" or "}}".

Why do we use string interpolation in C#?

C# string interpolation is used to format and manipulate strings. This feature was introduced in C# 6. Using string interpolation, we can use objects and expressions as a part of the string interpolation operation. C# string interpolation is a method of concatenating, formatting and manipulating strings.

Can we use expressions Inside string interpolation?

The string interpolation result is 'Hello, World!' . You can put any expression inside the placeholder: either an operator, a function call, or even more complex expressions. ${n1 + n2} is a placeholder consisting of the addition operator and 2 operands.

What is variable interpolation in C#?

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.


3 Answers

It's a compiler feature, not a framework feature. We successfully use both features with our .NET 3.5 projects in Visual Studio 2015.

In a nutshell, the compiler translates $"hello {person}" to String.Format("hello {0}", person) and nameof(arg1) to "arg1". It's just syntactic sugar.

The runtime sees a String.Format call (or a string literal "arg1", respectively) and does not know (nor care) what the original source code looked like. String.Format is supported since the early days of the .NET Framework, so there's nothing preventing you from targeting an earlier version of the framework.

like image 168
Heinzi Avatar answered Oct 06 '22 01:10

Heinzi


The existing answers talk about this being a C# 6 feature without a .NET framework component.

This is entirely true of nameof - but only somewhat true of string interpolation.

String interpolation will use string.Format in most cases, but if you're using .NET 4.6, it can also convert an interpolated string into a FormattableString, which is useful if you want invariant formatting:

using System;
using System.Globalization;
using static System.FormattableString;

class Test
{
    static void Main()
    {
        CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
        double d = 0.5;
        string local = $"{d}";
        string invariant = Invariant($"{d}");
        Console.WriteLine(local);     // 0,5
        Console.WriteLine(invariant); // 0.5
    }    
}

Obviously this wouldn't work if $"{d}" simply invoked a call to string.Format... instead, in this case it calls string.Format in the statement assigning to local, and calls FormattableStringFactory.Create in the statement assigning to invariant, and calls FormattableString.Invariant on the result. If you try to compile this against an earlier version of the framework, FormattableString won't exist so it won't compile. You can provide your own implementation of FormattableString and FormattableStringFactory if you really want to, though, and the compiler will use them appropriately.

like image 42
Jon Skeet Avatar answered Oct 06 '22 01:10

Jon Skeet


These things were introduced in C#6. .Net has nothing to do with it. So as long as you are using a C#6 compiler, you can use these features.

What is the difference between C# and .NET?

So yes, it is okay to use them in a project targeting .Net 4.5.

like image 24
Domysee Avatar answered Oct 06 '22 00:10

Domysee