IFormattable インターフェイスとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > 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'.
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「IFormattable インターフェイス」の関連用語

IFormattable インターフェイスのお隣キーワード
検索ランキング

   

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



IFormattable インターフェイスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS