Interpolated strings is a feature that allows you to embed local variables, instance variables or other expressions inside literal strings. X# supports two kinds of interpolated strings:
This works like a normal X# string but with an embedded expression:
FUNCTION Start AS VOID
LOCAL Who AS STRING
Who := "World"
Console.Writeline( i"Hello {Who}")
Console.Read()
RETURN
This is a combination of an interpolated string and an extended string. In the example below the \t will be replaced with a tab character:
FUNCTION Start AS VOID
LOCAL Who AS STRING
Who := "World"
Console.Writeline( ie"Hello\t{Who}")
Console.Read()
RETURN
Expression inside Interpolated strings inside X# have the form of
{<interpolationExpression>[,<alignment>][::<formatString>]}
In C# the formatString is prefixed with a single colon. Please note that because in X# the colon (:) operator is the default member access operator for instance fields, properties and methods, it is not always possible to use that operator to separate expressions and format string. We have therefore changed that operator to the double colon (::). I you compile with /allowdot+ then you can also use the single colon for the format separator.
To represent a literal '{' or '}' character you will have to duplicate them inside the string. The expressions inside the string are in italics in this example.
An example of this could be
FUNCTION Start() AS VOID
local X := 2 as INT
local Y := 3 as INT
var pointMessage := i"The point {{{X}, {Y}}} is {Math.Sqrt(X * X + Y * Y)::F3} from the origin"
Console.WriteLine(pointMessage)
// Output is:
// The point {2, 3} is 3.606 from the origin
The expression elements inside the string can use formatting notation just like the String.Format() notation. For example:
FUNCTION Start AS VOID
LOCAL i AS INT
i := 42
Console.Writeline( i"Hello {i:x}") // i is printed in hex notation, so Hello 2a
Console.Read()
RETURN