/
FormattableString.cs
87 lines (77 loc) · 3.21 KB
/
FormattableString.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics.CodeAnalysis;
namespace System
{
/// <summary>
/// Represents a composite format string, along with the arguments to be formatted.
/// </summary>
/// <remarks>
/// An instance of this type may result from the use of the C# or VB language primitive "interpolated string".
/// </remarks>
public abstract class FormattableString : IFormattable
{
/// <summary>
/// The composite format string.
/// </summary>
[StringSyntax(StringSyntaxAttribute.CompositeFormat)]
public abstract string Format { get; }
/// <summary>
/// Returns an object array that contains zero or more objects to format. Clients should not
/// mutate the contents of the array.
/// </summary>
public abstract object?[] GetArguments();
/// <summary>
/// The number of arguments to be formatted.
/// </summary>
public abstract int ArgumentCount { get; }
/// <summary>
/// Returns one argument to be formatted from argument position <paramref name="index"/>.
/// </summary>
public abstract object? GetArgument(int index);
/// <summary>
/// Format to a string using the given culture.
/// </summary>
public abstract string ToString(IFormatProvider? formatProvider);
string IFormattable.ToString(string? ignored, IFormatProvider? formatProvider) =>
ToString(formatProvider);
/// <summary>
/// Format the given object in the invariant culture. This static method may be
/// imported in C# by
/// <code>
/// using static System.FormattableString;
/// </code>.
/// Within the scope
/// of that import directive an interpolated string may be formatted in the
/// invariant culture by writing, for example,
/// <code>
/// Invariant($"{{ lat = {latitude}; lon = {longitude} }}")
/// </code>
/// </summary>
public static string Invariant(FormattableString formattable)
{
ArgumentNullException.ThrowIfNull(formattable);
return formattable.ToString(Globalization.CultureInfo.InvariantCulture);
}
/// <summary>
/// Format the given object in the current culture. This static method may be
/// imported in C# by
/// <code>
/// using static System.FormattableString;
/// </code>.
/// Within the scope
/// of that import directive an interpolated string may be formatted in the
/// current culture by writing, for example,
/// <code>
/// CurrentCulture($"{{ lat = {latitude}; lon = {longitude} }}")
/// </code>
/// </summary>
public static string CurrentCulture(FormattableString formattable)
{
ArgumentNullException.ThrowIfNull(formattable);
return formattable.ToString(Globalization.CultureInfo.CurrentCulture);
}
public override string ToString() =>
ToString(Globalization.CultureInfo.CurrentCulture);
}
}