You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: xml/System/ICustomFormatter.xml
+6Lines changed: 6 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -67,11 +67,13 @@
67
67
The following example implements <xref:System.ICustomFormatter> to allow binary, octal, and hexadecimal formatting of integral values. In this example, a single class, `IBinaryFormatter`, implements both <xref:System.ICustomFormatter> and <xref:System.IFormatProvider>. Its <xref:System.IFormatProvider.GetFormat%2A?displayProperty=nameWithType> method determines whether the `formatType` parameter represents an <xref:System.ICustomFormatter> type. If it does, `BinaryFormatter` returns an instance of itself; otherwise, it returns `null`. Its <xref:System.ICustomFormatter.Format%2A?displayProperty=nameWithType> implementation determines whether the format parameter is one of the three supported format strings ("B" for binary, "O" for octal, and "H" for hexadecimal) and formats the `arg` parameter appropriately. Otherwise, if `arg` is not `null`, it calls the `arg` parameter's <xref:System.IFormattable.ToString%2A?displayProperty=nameWithType> implementation, if one exists, or its parameterless `ToString` method, if one does not. If `arg` is `null`, the method returns <xref:System.String.Empty?displayProperty=nameWithType>.
`BinaryFormatter` can then be used to provide custom formatting by passing a `BinaryFormatter` object as the `provider` parameter of the <xref:System.String.Format%2A> method, as the following example shows.
<xref:System.ICustomFormatter.Format%2A?displayProperty=nameWithType> is a callback method. It is called by a method that supports custom formatting, such as <xref:System.String.Format%28System.IFormatProvider%2CSystem.String%2CSystem.Object%5B%5D%29?displayProperty=nameWithType> or <xref:System.Text.StringBuilder.AppendFormat%28System.IFormatProvider%2CSystem.String%2CSystem.Object%5B%5D%29?displayProperty=nameWithType>. The implementation is called once for each format item in a [composite format string](/dotnet/standard/base-types/composite-formatting). For example, in the following statement, the <xref:System.ICustomFormatter.Format%2A?displayProperty=nameWithType> method is called three times.
The `arg` parameter is the object in the object list whose zero-based position corresponds to the index of a particular format item.
@@ -171,6 +174,7 @@
171
174
Your implementation of the <xref:System.ICustomFormatter.Format%2A> method must include the following functionality so the .NET Framework can provide formatting you do not support. If your format method does not support a format, determine whether the object being formatted implements the <xref:System.IFormattable> interface. If it does, invoke the <xref:System.IFormattable.ToString%2A?displayProperty=nameWithType> method of that interface. Otherwise, invoke the default <xref:System.Object.ToString%2A?displayProperty=nameWithType> method of the underlying object. The following code illustrates this pattern.
The following example implements <xref:System.ICustomFormatter> to allow binary, octal, and hexadecimal formatting of integral values. Its <xref:System.ICustomFormatter.Format%2A?displayProperty=nameWithType> implementation determines whether the format parameter is one of the three supported format strings ("B" for binary, "O" for octal, and "H" for hexadecimal) and formats the `arg` parameter appropriately. Otherwise, if `arg` is not `null`, it calls the `arg` parameter's <xref:System.IFormattable.ToString%2A?displayProperty=nameWithType> implementation, if one exists, or its parameterless `ToString` method, if one does not. If `arg` is `null`, the method returns <xref:System.String.Empty?displayProperty=nameWithType>.
`BinaryFormatter` can then be used to provide custom formatting by passing a `BinaryFormatter` object as the `provider` parameter of the <xref:System.String.Format%2A> method, as the following example shows.
0 commit comments