IFormattableとは? わかりやすく解説

IFormattable インターフェイス

オブジェクトの値を文字列形式に変換する機能が用意されています。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<ComVisibleAttribute(True)> _
Public Interface IFormattable
[ComVisibleAttribute(true)] 
public interface IFormattable
[ComVisibleAttribute(true)] 
public interface class IFormattable
/** @attribute ComVisibleAttribute(true) */ 
public interface IFormattable
ComVisibleAttribute(true) 
public interface IFormattable
解説解説

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'.
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

IFormattable メソッド


パブリック メソッドパブリック メソッド

参照参照

関連項目

IFormattable インターフェイス
System 名前空間
IFormatProvider インターフェイス
Object.ToString
CurrentCulture

その他の技術情報

書式設定の概要

IFormattable メンバ

オブジェクトの値を文字列形式に変換する機能が用意されています。

IFormattable データ型で公開されるメンバを以下の表に示します。


パブリック メソッドパブリック メソッド
参照参照

関連項目

IFormattable インターフェイス
System 名前空間
IFormatProvider インターフェイス
Object.ToString
CurrentCulture

その他の技術情報

書式設定の概要



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「IFormattable」の関連用語

IFormattableのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



IFormattableのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS