-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathIFormattable.xml
225 lines (182 loc) · 19.3 KB
/
IFormattable.xml
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<Type Name="IFormattable" FullName="System.IFormattable">
<TypeSignature Language="C#" Value="public interface IFormattable" />
<TypeSignature Language="ILAsm" Value=".class public interface auto ansi abstract IFormattable" FrameworkAlternate="dotnet-uwp-10.0;net-5.0;net-6.0;net-7.0;netcore-1.0;netcore-1.1;netcore-2.0;netcore-2.1;netcore-2.2;netcore-3.0;netcore-3.1;netframework-1.1;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1;netstandard-1.0;netstandard-1.1;netstandard-1.2;netstandard-1.3;netstandard-1.4;netstandard-1.5;netstandard-1.6;netstandard-2.0;netstandard-2.1" />
<TypeSignature Language="DocId" Value="T:System.IFormattable" />
<TypeSignature Language="VB.NET" Value="Public Interface IFormattable" />
<TypeSignature Language="F#" Value="type IFormattable = interface" />
<TypeSignature Language="C++ CLI" Value="public interface class IFormattable" />
<TypeSignature Language="ILAsm" Value=".class public interface auto ansi abstract beforefieldinit IFormattable" FrameworkAlternate="net-8.0;net-9.0" />
<AssemblyInfo>
<AssemblyName>System.Runtime</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.0.20.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.2.0.0</AssemblyVersion>
<AssemblyVersion>4.2.1.0</AssemblyVersion>
<AssemblyVersion>4.2.2.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<TypeForwardingChain>
<TypeForwarding From="mscorlib" FromVersion="4.0.0.0" To="System.Runtime" ToVersion="0.0.0.0" FrameworkAlternate="dotnet-uwp-10.0" />
<TypeForwarding From="netstandard" FromVersion="2.1.0.0" To="System.Runtime" ToVersion="5.0.0.0" FrameworkAlternate="net-5.0" />
<TypeForwarding From="netstandard" FromVersion="2.1.0.0" To="System.Runtime" ToVersion="6.0.0.0" FrameworkAlternate="net-6.0" />
<TypeForwarding From="netstandard" FromVersion="2.1.0.0" To="System.Runtime" ToVersion="7.0.0.0" FrameworkAlternate="net-7.0" />
<TypeForwarding From="netstandard" FromVersion="2.1.0.0" To="System.Runtime" ToVersion="8.0.0.0" FrameworkAlternate="net-8.0" />
<TypeForwarding From="netstandard" FromVersion="2.1.0.0" To="System.Runtime" ToVersion="9.0.0.0" FrameworkAlternate="net-9.0" />
<TypeForwarding From="System.Runtime" FromVersion="4.1.1.1" To="mscorlib" ToVersion="4.0.0.0" FrameworkAlternate="netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1" />
</TypeForwardingChain>
<Interfaces />
<Attributes>
<Attribute FrameworkAlternate="net-8.0;net-9.0">
<AttributeName Language="C#">[System.Runtime.CompilerServices.NullableContext(2)]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.CompilerServices.NullableContext(2)>]</AttributeName>
</Attribute>
<Attribute FrameworkAlternate="netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1">
<AttributeName Language="C#">[System.Runtime.InteropServices.ComVisible(true)]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.InteropServices.ComVisible(true)>]</AttributeName>
</Attribute>
</Attributes>
<Docs>
<summary>Provides functionality to format the value of an object into a string representation.</summary>
<remarks>
<format type="text/markdown"><).
- Standard and custom format strings for formatting numeric values (see [Standard Numeric Format Strings](/dotnet/standard/base-types/standard-numeric-format-strings) and [Custom Numeric Format Strings](/dotnet/standard/base-types/custom-numeric-format-strings)).
- Standard and custom format strings for formatting date and time values (see [Standard Date and Time Format Strings](/dotnet/standard/base-types/standard-date-and-time-format-strings) and [Custom Date and Time Format Strings](/dotnet/standard/base-types/custom-date-and-time-format-strings)).
- Standard and custom format strings for formatting time intervals (see [Standard TimeSpan Format Strings](/dotnet/standard/base-types/standard-timespan-format-strings) and [Custom TimeSpan Format Strings](/dotnet/standard/base-types/custom-timespan-format-strings)).
You can also define your own format strings to support formatting of your application-defined types.
A format provider returns a formatting object that typically defines the symbols used in converting an object to its string representation. For example, when you convert a number to a currency value, a format provider defines the currency symbol that appears in the result string. The .NET Framework defines three format providers:
- The <xref:System.Globalization.CultureInfo?displayProperty=nameWithType> class, which returns either a <xref:System.Globalization.NumberFormatInfo> object for formatting numeric values, or a <xref:System.Globalization.DateTimeFormatInfo> object for formatting date and time values.
- The <xref:System.Globalization.NumberFormatInfo?displayProperty=nameWithType> class, which returns an instance of itself for formatting numeric values.
- The <xref:System.Globalization.DateTimeFormatInfo?displayProperty=nameWithType> class, which returns an instance of itself for formatting date and time values.
In addition, you can define your own custom format providers to supply culture-specific, profession-specific, or industry-specific information used in formatting. For more information about implementing custom formatting by using a custom format provider, see <xref:System.ICustomFormatter>.
The <xref:System.IFormattable> interface defines a single method, <xref:System.IFormattable.ToString%2A>, that supplies formatting services for the implementing type. The <xref:System.IFormattable.ToString%2A> method can be called directly. In addition, it is called automatically by the <xref:System.Convert.ToString%28System.Object%29?displayProperty=nameWithType> and <xref:System.Convert.ToString%28System.Object%2CSystem.IFormatProvider%29?displayProperty=nameWithType> methods, and by methods that use the [composite formatting feature](/dotnet/standard/base-types/composite-formatting) in the .NET Framework. Such methods include <xref:System.Console.WriteLine%28System.String%2CSystem.Object%29?displayProperty=nameWithType>, <xref:System.String.Format%2A?displayProperty=nameWithType>, and <xref:System.Text.StringBuilder.AppendFormat%28System.String%2CSystem.Object%29?displayProperty=nameWithType>, among others. The <xref:System.IFormattable.ToString%2A> method is called for each format item in the method's format string.
The <xref:System.IFormattable> interface is implemented by the base data types.
## Examples
The following example defines a `Temperature` class that implements the <xref:System.IFormattable> interface. The class supports four format specifiers: "G" and "C", which indicate that the temperature is to be displayed in Celsius; "F", which indicates that the temperature is to be displayed in Fahrenheit; and "K", which indicates that the temperature is to be displayed in Kelvin. In addition, the <xref:System.IFormattable.ToString%2A?displayProperty=nameWithType> implementation also can handle a format string that is `null` or empty. The other two `ToString` methods defined by the `Temperature` class simply wrap a call to the <xref:System.IFormattable.ToString%2A?displayProperty=nameWithType> implementation.
:::code language="csharp" source="~/snippets/csharp/System/IFormattable/Overview/example1.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/IFormattable/Overview/example1.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.iformattable/vb/example1.vb" id="Snippet1":::
The following example then calls the <xref:System.IFormattable.ToString%2A?displayProperty=nameWithType> implementation either directly or by using a composite format string.
:::code language="csharp" source="~/snippets/csharp/System/IFormattable/Overview/example1.cs" id="Snippet2":::
:::code language="fsharp" source="~/snippets/fsharp/System/IFormattable/Overview/example1.fs" id="Snippet2":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.iformattable/vb/example1.vb" id="Snippet2":::
]]></format>
</remarks>
<block subset="none" type="overrides">
<para>Classes that require more control over the formatting of strings than <see cref="M:System.Object.ToString" /> provides should implement <see cref="T:System.IFormattable" />.
A class that implements <see cref="T:System.IFormattable" /> must support the "G" (general) format specifier. Besides the "G" specifier, the class can define the list of format specifiers that it supports. In addition, the class must be prepared to handle a format specifier that is <see langword="null" />. For more information about formatting and formatting codes, see <see href="/dotnet/standard/base-types/formatting-types">Formatting Types</see></para>
</block>
<altmember cref="T:System.IFormatProvider" />
<altmember cref="M:System.Object.ToString" />
<altmember cref="P:System.Threading.Thread.CurrentCulture" />
<related type="Article" href="/dotnet/standard/base-types/formatting-types">Formatting Types in .NET</related>
</Docs>
<Members>
<Member MemberName="ToString">
<MemberSignature Language="C#" Value="public string ToString (string format, IFormatProvider formatProvider);" FrameworkAlternate="dotnet-uwp-10.0;netcore-1.0;netcore-1.1;netcore-2.0;netcore-2.1;netcore-2.2;netframework-1.1;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1;netstandard-1.0;netstandard-1.1;netstandard-1.2;netstandard-1.3;netstandard-1.4;netstandard-1.5;netstandard-1.6;netstandard-2.0;netstandard-2.1" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig newslot virtual instance string ToString(string format, class System.IFormatProvider formatProvider) cil managed" />
<MemberSignature Language="DocId" Value="M:System.IFormattable.ToString(System.String,System.IFormatProvider)" />
<MemberSignature Language="VB.NET" Value="Public Function ToString (format As String, formatProvider As IFormatProvider) As String" />
<MemberSignature Language="F#" Value="abstract member ToString : string * IFormatProvider -> string" Usage="iFormattable.ToString (format, formatProvider)" />
<MemberSignature Language="C++ CLI" Value="public:
 System::String ^ ToString(System::String ^ format, IFormatProvider ^ formatProvider);" />
<MemberSignature Language="C#" Value="public string ToString (string? format, IFormatProvider? formatProvider);" FrameworkAlternate="net-5.0;net-6.0;net-7.0;net-8.0;net-9.0;netcore-3.0;netcore-3.1" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Runtime</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.0.20.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.2.0.0</AssemblyVersion>
<AssemblyVersion>4.2.1.0</AssemblyVersion>
<AssemblyVersion>4.2.2.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.String</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="format" Type="System.String" />
<Parameter Name="formatProvider" Type="System.IFormatProvider" />
</Parameters>
<Docs>
<param name="format">The format to use.
-or-
A null reference (<see langword="Nothing" /> in Visual Basic) to use the default format defined for the type of the <see cref="T:System.IFormattable" /> implementation.</param>
<param name="formatProvider">The provider to use to format the value.
-or-
A null reference (<see langword="Nothing" /> in Visual Basic) to obtain the numeric format information from the current locale setting of the operating system.</param>
<summary>Formats the value of the current instance using the specified format.</summary>
<returns>The value of the current instance in the specified format.</returns>
<remarks>
<format type="text/markdown"><.)
Composite formatting methods call the <xref:System.IFormattable.ToString%2A> method once for each format item in a format string. The parameters passed to the method depend on the specific formatting method that is called and on the content of the format item, as follows:
- If the format item does not include a format string (for example, if the format item is simply `{0}`), it is passed `null` as the value of the <xref:System.String?displayProperty=nameWithType> parameter.
- If the format item includes a format string (for example, `{0:G}`), that format string is passed as the value of the <xref:System.String?displayProperty=nameWithType> parameter.
- If the original method call does not include an <xref:System.IFormatProvider?displayProperty=nameWithType> parameter, <xref:System.Globalization.CultureInfo.CurrentCulture%2A?displayProperty=nameWithType> is passed as the value of the <xref:System.IFormatProvider?displayProperty=nameWithType> parameter.
- If the original method call includes an <xref:System.IFormatProvider?displayProperty=nameWithType> parameter, the provider that is supplied in the method call is passed as the value of the <xref:System.IFormatProvider?displayProperty=nameWithType> parameter.
> [!NOTE]
> An object's <xref:System.IFormattable.ToString%2A> implementation is called by composite formatting methods only if they are not passed an <xref:System.ICustomFormatter> format provider, or if the <xref:System.ICustomFormatter.Format%2A> method of the custom format provider returns `null`.
The .NET Framework includes three format providers, all of which implement the <xref:System.IFormatProvider> interface:
- <xref:System.Globalization.NumberFormatInfo> supplies numeric formatting information, such as the characters to use for decimal and group separators, and the spelling and placement of currency symbols in monetary values.
- <xref:System.Globalization.DateTimeFormatInfo> supplies date-related and time-related formatting information, such as the position of the month, the day, and the year in a date pattern.
- <xref:System.Globalization.CultureInfo> contains the default formatting information in a specific culture, including the numeric format information, and date-related and time-related formatting information.
In addition, you can define your own custom format provider.
## Examples
The following example demonstrates a `Temperature` class that implements the <xref:System.IFormattable.ToString%2A> method. This code example is part of a larger example provided for the <xref:System.IFormattable> class.
:::code language="csharp" source="~/snippets/csharp/System/IFormattable/Overview/example1.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/IFormattable/Overview/example1.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.iformattable/vb/example1.vb" id="Snippet1":::
]]></format>
</remarks>
<block subset="none" type="overrides">
<para>The <see cref="M:System.IFormattable.ToString(System.String,System.IFormatProvider)" /> method must support the "G" (general) format specifier. Besides the "G" specifier, the class can define the list of format specifiers that it supports. In addition, the class must be prepared to handle a format specifier that is <see langword="null" />. For more information about formatting and formatting codes, see <see href="/dotnet/standard/base-types/formatting-types">Formatting Types</see>.</para>
</block>
<altmember cref="T:System.String" />
<altmember cref="T:System.IFormatProvider" />
<altmember cref="T:System.Globalization.NumberFormatInfo" />
<altmember cref="T:System.Globalization.DateTimeFormatInfo" />
<altmember cref="T:System.Globalization.CultureInfo" />
</Docs>
</Member>
</Members>
</Type>