SoFunction
Updated on 2025-03-01

How to use Interpolated Strings in C# 6.0

Before looking at Interpolated Strings, let's look at a new feature of EF Core 2.0:String interpolation in FromSql and

ExecuteSqlCommand。

var city = "London";

using (var context = CreateContext())
{
 
  .FromSql($@"
   SELECT *
   FROM Customers
   WHERE City = {city}")
  .ToArray();
}

SQL statements are executed in a parameterized manner, so they are anti-string injection.

@p0='London' (Size = 4000)

SELECT *
FROM Customers
WHERE City = @p0

Always thinkInterpolated StringsonlyThe syntax sugar, passed toFromSqlThe method is just a normal string, curly braces have been removed and the variable has been replaced with the corresponding value.FromSqlIf variable information cannot be obtained, how can I implement parameterized query? OK, let's start from scratch.

What are Interpolated Strings

Interpolated strings are a new syntax introduced in C# 6.0, which allows insertion of expressions into strings.

var name = "world";
($"hello {name}");

This method is relatively similar to the previous oneorEasier to write and more readable. This is enough to satisfy most people. In fact, it's more than just a simple string.

What is Interpolated Strings?

Use code to answer this question:

var name = "world";
string str1 = $"hello {name}"; //Equal to var str1 = $"hello {name}";IFormattable str2 = $"hello {name}";
FormattableString str3 = $"hello {name}";

As can be seen, Interpolated Strings can be implicitly converted into 3 forms. In fact, the compiler silently made conversions for us:

var name = "world";
string str1 = ("hello {0}",name); //Equal to var str1 = $"hello {name}";IFormattable str2 = ("hello {0}",name);
FormattableString str3 = ("hello {0}",name);
  • IFormattableThere has been one since the .net Framework 1 era, only oneToStringMethod, can be passed inIFormatProviderTo control the formatting of strings. Today's protagonist is not him.
  • FormattableStringNew classes introduced with Interpolated Strings.

What is FormattableString?

Let's look at a piece of code first

var name = "world";
FormattableString fmtString = $"hello {name}";
(); //1
(); //hello {0}
foreach (var arg in ())
{
 (arg); //world
 (()); //
}

It can be seenFormattableStringAll information about Interpolated Strings is saved, so EF Core 2.0 can execute SQL in a parameterized manner.

Things to note in EF Core

Because of the implicit conversion,Use EF CoreofFromSql Methods andExecuteSqlCommandSpecial care is required when doing the method. If you are not careful, you will get into a trap.

var city = "London";

using (var context = CreateContext())
{
 //Method 1, non-parametric var sql = $" SELECT * FROM Customers WHERE City = {city}";
 (sql).ToArray();

 //Method 2, parameterization ($" SELECT * FROM Customers WHERE City = {city}").ToArray();

 //Method 3, Parameterization FormattableString fsql = $" SELECT * FROM Customers WHERE City = {city}";
 (fsql).ToArray();

 //Method 4, non-parametric var sql = " SELECT * FROM Customers WHERE City = @p0";
 (sql, city).ToArray();

}

The first method is that the SQL assignment is compiled intoThe method call returns a string. When the sql variable is passed into the FromSql method, it passes again arriveImplicit conversion. However, the SQL variable itself has lost parameter information, so parameterized query cannot be implemented.

The fourth method is also the conversion process of Interpolated Strings -> String -> RawSqlString, but because variables are passed into the FromSql method separately, they are executed in a parameterized manner.

other

Students who are familiar with ES2015 can take a look at the implementation in Javascript.Tagged template literals, which is very similar to Interpolated Strings.

I posted at 12 a.m. last night, and I don’t know why my homepage was removed. I feel that it is because of insufficient space. I added some EF Core precautions, but I couldn't go back to the homepage after more than 1 hour. The first article in seven years is a bit regrettable. Hope everyone likes it.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.