最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

.net - Does string interpolation work the same for C# and F#? - Stack Overflow

programmeradmin2浏览0评论

I expected string interpolation to be the exact same for F# and C#.

This is fine for C#:

var x = $"{123:00000000}";

This does not work for F#:

printfn $"{123:00000000}"

Looking for an explanation.

I would expect MS docs to have a "F#/C# interpolation differences" but it ain't there

I expected string interpolation to be the exact same for F# and C#.

This is fine for C#:

var x = $"{123:00000000}";

This does not work for F#:

printfn $"{123:00000000}"

Looking for an explanation.

I would expect MS docs to have a "F#/C# interpolation differences" but it ain't there https://learn.microsoft/en-us/dotnet/fsharp/language-reference/interpolated-strings

Share Improve this question asked Mar 7 at 8:12 inwenisinwenis 4078 silver badges26 bronze badges 5
  • 1 "I expected string interpolation to be the exact same for F# and C#." They're just not. Even a quick look at the syntax of the F# interpolated strings shows %format-specifier which wouldn't be present in C#. I would say it's very rarely a good idea to expect anything to work exactly the same between two languages. – Jon Skeet Commented Mar 7 at 8:25
  • 1 "Looking for an explanation." You've linked to the documentation, which includes multiple examples of using format specifiers. That is the explanation. I'm not sure what you'd expect to see in an answer that isn't in the docs. – Jon Skeet Commented Mar 7 at 8:26
  • 2 F# and C#, while both running on .NET and both being developed and maintained by Microsoft, are two different languages. Expecting things to be the exact same between F# and C# is like expecting things to be the exact same between Java and Scala. Why would Microsoft write up a single page on how string interpolation works in C# and in F# highlighting the differences, if instead they can simply write one page for C#, one for F# and let the reader grasp the obvious differences between them? – MindSwipe Commented Mar 7 at 8:39
  • 1 If you scroll down to the Format Specifiers section, it shows that for custom formats, you need to quote them in backticks `` ``. – Jeff Mercado Commented Mar 7 at 8:40
  • Thank you for the comments. I will read the docs more diligently. – inwenis Commented Mar 7 at 9:37
Add a comment  | 

2 Answers 2

Reset to default 5

Does string interpolation work the same for C# and F#?

No! Or rather, "not quite".

This is covered by the documentation.

Short version, if the format specifier contains "unusual characters" then it needs to be quoted with double back ticks.

Eg. (from the docs)

let nowDashes = $"{System.DateTime.UtcNow:``yyyy-MM-dd``}" // e.g. "2022-02-10"

For completeness: F# supports three types of string formatting

  • Plain text formatting: inbuilt, type-safe (checked by the compiler and tools). There are many methods supporting this (in F#'s core library's Printf Module).
  • String Interpolation: as above. Something of a hybrid of C#'s interpolated strings and F#'s plain text formatting.
  • You can of course call String.Format (et. al.) directly.

F# supports string interpolation, but it does not use string.format internally. Use printfn $"{123.ToString("00000000")}" instead, beacuase F# needs a more explicit conversion. Hope this helps

发布评论

评论列表(0)

  1. 暂无评论