MSDN: FormattableString Class
Questions tagged [formattablestring]
10 questions
57
votes
2 answers
Difference between String, FormattableString, IFormattable
FormattableString has been Introduced in C# 6.0. As we can use same string formatting using string object why is there need of using FormattableString or IFormattable. Whats difference between three?
My Code
var name = "Pravin";
var…

Nitin Varpe
- 10,450
- 6
- 36
- 60
15
votes
1 answer
C# FormattableString concatenation for multiline interpolation
In C#7, I'm trying to use a multiline interpolated string for use with FormttableString.Invariant but string concatenation appears to be invalid for FormttableString.
Per the documentation: A FormattableString instance may result from an…

Eugene
- 10,957
- 20
- 69
- 97
10
votes
2 answers
C# Serilog: how to log with String interpolation and keep argument names in message templates?
How to replace this code:
string name = "John";
logger.Information("length of name '{name}' is {nameLength}", name, name.Length);
with C# String interpolation like this or similar
string name = "John";
// :-( lost benefit of structured logging:…

Artemious
- 1,980
- 1
- 20
- 31
10
votes
1 answer
C# Convert string variable to FormattableString
Suppose, in a C# program, I have the following lines in my app.config:
And, in my code, I am using it as follows:
private void doStuff()
{
…

John Bustos
- 19,036
- 17
- 89
- 151
7
votes
3 answers
Interpolated strings in F#
I am trying to use the Entity Framework Core interpolated SQL query function in F# which requires a FormattableString. However to my surprise it doesn't function as I cannot find a way to convert a regular F# string to that type. I figured just…

manywows
- 195
- 1
- 8
3
votes
1 answer
Not able to use FormattableString
Reading this interesting article about SQL injection prevention in EF Core, I found out that now an interpolated string may result in a FormattableString.
Running this test code in .NET Core 2.2:
public static void Main()
{
var filter = "Mark';…

Teejay
- 7,210
- 10
- 45
- 76
2
votes
1 answer
C# question: Why cannot I assign a string directly to a class inheriting System.IFormattable or System.FormattableString while they themselves can?
public class Program
{
public void Main()
{
IFormattable string1 = $"{1},{"test"}"; //Works
FormattableString string2 = $"{1},{"test"}"; //Works
LocStr string3 = $"{1},{"test"}"; ///Error: Cannot implicitly…

osman doluca
- 23
- 3
2
votes
2 answers
Convert string to FormattableString in Entity Framework
I am trying to build a website vulnerable to SQL injections (for educational purposes) with an ASP.NET API. To do that, I would like to call FromSql with a previously prepared SQL query as such:
String query = "SELECT * FROM users WHERE email =…

CSNooB
- 39
- 6
1
vote
2 answers
A string with string interpolation format from database use it in C# code
For example I have a string that was retrieved from the database.
string a = "The quick brown {Split[0]} {Split[1]} the lazy dog";
string b = "jumps over";
And then I will perform this code.
String[] Split= b.Split(' ');
String c=…
0
votes
1 answer
How to use FormattableString[] in TestCaseSource
I have an NUnit test that needs to test a bunch of different FormattableString cases and I'm passing the cases using TestCaseSource:
static IEnumerable TestCaseSourceData()
{
FormattableString data1 =…

Bizhan
- 16,157
- 9
- 63
- 101