Share via


IFormattable-Schnittstelle

Stellt Funktionen bereit, mit denen der Wert eines Objekts in eine Zeichenfolgenentsprechung formatiert werden kann.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public Interface IFormattable
'Usage
Dim instance As IFormattable
[ComVisibleAttribute(true)] 
public interface IFormattable
[ComVisibleAttribute(true)] 
public interface class IFormattable
/** @attribute ComVisibleAttribute(true) */ 
public interface IFormattable
ComVisibleAttribute(true) 
public interface IFormattable

Hinweise

IFormattable wird von den Basisdatentypen implementiert.

Ein Format beschreibt die Darstellung eines Objekts nach der Konvertierung in eine Zeichenfolge. Ein Format kann vorgegeben oder benutzerdefiniert sein. Ein Standardformat hat die Form Axx, wobei A der Formatbezeichner, d. h. ein Buchstabe, und xx die Genauigkeitsangabe, d. h. eine nicht negative ganze Zahl ist. Der Formatbezeichner steuert die Art der Formatierung, die auf den als Zeichenfolge dargestellten Wert angewendet wird. Die Genauigkeitsangabe legt ggf. die Anzahl der signifikanten Stellen bzw. Dezimalstellen in der Zeichenkette fest.

Wenn ein Format kulturabhängige Symbole enthält, z. B. das von den "C"- und "c"-Formaten dargestellte Währungssymbol, werden die tatsächlich in der Zeichenfolgenentsprechung verwendeten Zeichen von einem Formatierungsobjekt geliefert. Eine Methode kann einen Parameter zur Übergabe eines IFormatProvider-Objekts enthalten, das ein Formatierungsobjekt bereitstellt, oder die Methode kann das Standardformatierungsobjekt verwenden, das die Symboldefinitionen für den aktuellen Thread enthält. Der aktuelle Thread verwendet i. d. R. die systemweit standardmäßig verwendete Symbolmenge.

Hinweise für Implementierer Klassen, die mehr Einfluss auf die Formatierung von Zeichenketten benötigen, als von Object.ToString bereitgestellt wird, müssen IFormattable implementieren, deren ToString-Methode die CurrentCulture-Eigenschaft des aktuellen Threads verwendet. Eine Klasse, die IFormattable implementiert, muss den Formatierungscode "G" (general: allgemein) unterstützen. Außer dem Code "G" kann die Klasse eine Liste von unterstützten Formatierungscodes definieren. Weitere Informationen über Formatierung und Formatierungscodes finden Sie unter Übersicht über Formatierung.

Beispiel

Im folgenden Beispiel wird die Definition eines Typs veranschaulicht, der die IFormattable-Schnittstelle implementiert. Im Beispiel wird auch veranschaulicht, wie die ToString-Methode der IFormattable-Schnittstelle aufgerufen wird.

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'.

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

IFormattable-Member
System-Namespace
IFormatProvider-Schnittstelle
Object.ToString
CurrentCulture

Weitere Ressourcen

Übersicht über Formatierung