IFormattable インターフェイス
アセンブリ: mscorlib (mscorlib.dll 内)


IFormattable は、基本データ型により実装されます。
オブジェクトを文字列へ変換するときに、書式によりオブジェクトの外観が記述されます。標準書式とカスタム書式のいずれかを使用できます。標準書式は Axx という形をとります。A は書式指定子と呼ばれるアルファベット文字、xx は有効桁数指定子と呼ばれる正の整数値です。書式指定子は、文字列として表される値に適用される書式の種類を制御します。有効桁数指定子は、文字列の有効桁数または小数部の桁数を制御します。
"C" または "c" 書式で表される通貨記号など、カルチャによって異なる記号が書式に含まれている場合、文字列形式で使用される実際の文字は、書式指定オブジェクトにより指定されます。メソッドでは、書式指定オブジェクトを提供する IFormatProvider オブジェクトを渡すためのパラメータが指定されているか、現在のスレッドのシンボル定義が格納されている既定の書式設定オブジェクトが使用されます。通常、現在のスレッドは、システム全体で使用される既定のシンボル セットを使用します。
実装時の注意 Object.ToString よりも文字列の書式指定を厳密に制御する必要があるクラスは、IFormattable を実装する必要があります。ToString メソッドは、現在のスレッドの CurrentCulture プロパティを使用します。 IFormattable を実装するクラスでは、書式指定コード "G" (一般) をサポートしている必要があります。このクラスは "G" コード以外にも、使用できる書式指定コードのリストを定義できます。 書式指定および書式指定コードの詳細については、「書式設定の概要」を参照してください。
IFormattable インターフェイスを実装する型を定義する例を次に示します。この例では、IFormattable インターフェイスの ToString メソッドを呼び出す方法も示されています。
using System; class Point : IFormattable { public int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public override String ToString() { return ToString(null, null); } public String ToString(String format, IFormatProvider fp) { // If no format is passed, display like this: (x, y). if (format == null) return String.Format("({0}, {1})", x, y); // For "x" formatting, return just the x value as a string if (format == "x") return x.ToString(); // For "y" formatting, return just the y value as a string if (format == "y") return y.ToString(); // For any unrecognized format, throw an exception. throw new FormatException(String.Format("Invalid format string: '{0}'.", format)); } } public sealed class App { static void Main() { // Create the object. Point p = new Point(5, 98); // Test ToString with no formatting. Console.WriteLine("This is my point: " + p.ToString()); // Use custom formatting style "x" Console.WriteLine("The point's x value is {0:x}", p); // Use custom formatting style "y" Console.WriteLine("The point's y value is {0:y}", p); try { // Use an invalid format; FormatException should be thrown here. Console.WriteLine("Invalid way to format a point: {0:XYZ}", p); } catch (FormatException e) { Console.WriteLine("The last line could not be displayed: {0}", e.Message); } } } // This code produces the following output. // // This is my point: (5, 98) // The point's x value is 5 // The point's y value is 98 // The last line could not be displayed: Invalid format string: 'XYZ'.
using namespace System; public value struct Point : IFormattable { private: int x; private: int y; public: property int X { int get() { return x; } void set(int value) { x = value; } } public: property int Y { int get() { return y; } void set(int value) { y = value; } } public: Point(int x, int y) { this->x = x; this->y = y; } public: virtual String^ ToString() override { return ToString(nullptr, nullptr); } public: virtual String^ ToString(String^ format, IFormatProvider^ formatProvider) { // If no format is passed, display like this: (x, y). if (format == nullptr) { return String::Format("({0}, {1})", x, y); } // For "x" formatting, return just the x value as a string if (format == "x") { return x.ToString(); } // For "y" formatting, return just the y value as a string if (format == "y") { return y.ToString(); } // For any unrecognized format, throw an exception. throw gcnew FormatException(String::Format( "Invalid format string: '{0}'.", format)); } }; int main() { // Create the object. Point p = Point(5, 98); // Test ToString with no formatting. Console::WriteLine("This is my point: " + p.ToString()); // Use custom formatting style "x" Console::WriteLine("The point's x value is {0:x}", p); // Use custom formatting style "y" Console::WriteLine("The point's y value is {0:y}", p); try { // Use an invalid format; // FormatException should be thrown here. Console::WriteLine( "Invalid way to format a point: {0:XYZ}", p); } catch (FormatException^ e) { Console::WriteLine( "The last line could not be displayed: {0}", e->Message); } } // This code produces the following output. // // This is my point: (5, 98) // The point's x value is 5 // The point's y value is 98 // The last line could not be displayed: Invalid format string: 'XYZ'.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- IFormattable インターフェイスのページへのリンク