Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Interpolation in Visual Studio 2015 and IFormatProvider (CA1305)

The new string interpolation style in Visual Studio 2015 is this:

Dim s = $"Hello {name}" 

But if I use this the code analysis tells me I break CA1305: Specify IFormatProvider

In the old times I did it like this:

Dim s = String.Format(Globalization.CultureInfo.InvariantCulture, "Hello {0}", name) 

But how can it be done with the new style?

I have to mention that I'm looking for a solution for .Net 4.5.2 (for .Net 4.6 dcastro has the answer)

like image 958
habakuk Avatar asked Aug 18 '15 15:08

habakuk


People also ask

How do I use IFormatProvider?

The class that implements IFormatProvider can then be used in a call to a formatting and parsing operation. For example, the following code calls the String. Format(IFormatProvider, String, Object[]) method to generate a string that contains a formatted 12-digit account number. using System; using System.

What is string interpolation in C# example?

An interpolated string is a string literal that might contain interpolation expressions. When an interpolated string is resolved to a result string, items with interpolation expressions are replaced by the string representations of the expression results. This feature is available starting with C# 6.

What is interpolation 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.


2 Answers

You'd use the System.FormattableString or System.IFormattable class:

IFormattable ifs = (IFormattable)$"Hello, {name}"; System.FormattableString fss = $"Hello, {name}";  // pass null to use the format as it was used upon initialization above. string ifresult = ifs.ToString(null, CultureInfo.InvariantCulture); string fsresult = fss.ToString(CultureInfo.InvariantCulture); 

You need to be compiling against Framework 4.6, as the IFormattable and FormattableString are classes which do not exist in older versions. So if you're targeting older versions of the .NET framework you can't use the interpolation syntax without triggering the error.

Unless you apply a little hack (adapted to compile against 4.6 RTM from Jon Skeet's gist and forked to my own account.). Just add a class file to your project containing:

Update

There is now also a Nuget package available that will provide the same functionality to your project (thanks for bringing this to my attention @habakuk).

install-package StringInterpolationBridge 

Or if you want to achieve the same thing without adding an additional assembly to your product add the following code to your project:

namespace System.Runtime.CompilerServices {     internal class FormattableStringFactory     {         public static FormattableString Create(string messageFormat, params object[] args)         {             return new FormattableString(messageFormat, args);         }     } }  namespace System {     internal class FormattableString : IFormattable     {         private readonly string messageFormat;         private readonly object[] args;          public FormattableString(string messageFormat, object[] args)         {             this.messageFormat = messageFormat;             this.args = args;         }          public override string ToString()         {             return string.Format(messageFormat, args);         }          public string ToString(string format, IFormatProvider formatProvider)         {             return string.Format(formatProvider, format ?? messageFormat, args);         }          public string ToString(IFormatProvider formatProvider)         {             return string.Format(formatProvider, messageFormat, args);         }     } } 

See:

  • https://msdn.microsoft.com/en-us/library/dn961160.aspx
like image 187
jessehouwing Avatar answered Oct 08 '22 13:10

jessehouwing


If you're targeting the .NET Framework 4.6, you can take advantage of the fact that string interpolations are implicitly convertible to FormattableString:

From Customizing string interpolation in C# 6 by Thomas Levesque

A lesser known aspect of this feature is that an interpolated string can be treated either as a String, or as an IFormattable, depending on the context.

static string Invariant(FormattableString formattable) {     return formattable.ToString(CultureInfo.InvariantCulture); }  string text = Invariant($"{p.Name} was born on {p.DateOfBirth:D}"); 
like image 25
dcastro Avatar answered Oct 08 '22 12:10

dcastro