1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 /*============================================================
9 ** Purpose: implementation of the FormattableString
12 ===========================================================*/
17 /// A composite format string along with the arguments to be formatted. An instance of this
18 /// type may result from the use of the C# or VB language primitive "interpolated string".
20 public abstract class FormattableString
: IFormattable
23 /// The composite format string.
25 public abstract string Format { get; }
28 /// Returns an object array that contains zero or more objects to format. Clients should not
29 /// mutate the contents of the array.
31 public abstract object?[] GetArguments();
34 /// The number of arguments to be formatted.
36 public abstract int ArgumentCount { get; }
39 /// Returns one argument to be formatted from argument position <paramref name="index"/>.
41 public abstract object? GetArgument(int index
);
44 /// Format to a string using the given culture.
46 public abstract string ToString(IFormatProvider
? formatProvider
);
48 string IFormattable
.ToString(string? ignored
, IFormatProvider
? formatProvider
)
50 return ToString(formatProvider
);
54 /// Format the given object in the invariant culture. This static method may be
57 /// using static System.FormattableString;
60 /// of that import directive an interpolated string may be formatted in the
61 /// invariant culture by writing, for example,
63 /// Invariant($"{{ lat = {latitude}; lon = {longitude} }}")
66 public static string Invariant(FormattableString formattable
)
68 if (formattable
== null)
70 throw new ArgumentNullException(nameof(formattable
));
73 return formattable
.ToString(Globalization
.CultureInfo
.InvariantCulture
);
77 /// Format the given object in the current culture. This static method may be
80 /// using static System.FormattableString;
83 /// of that import directive an interpolated string may be formatted in the
84 /// current culture by writing, for example,
86 /// CurrentCulture($"{{ lat = {latitude}; lon = {longitude} }}")
89 public static string CurrentCulture(FormattableString formattable
)
91 if (formattable
== null)
93 throw new ArgumentNullException(nameof(formattable
));
96 return formattable
.ToString(Globalization
.CultureInfo
.CurrentCulture
);
99 public override string ToString()
101 return ToString(Globalization
.CultureInfo
.CurrentCulture
);