IFormattable 接口

提供将对象的值格式化为字符串表示形式的功能。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
Public Interface IFormattable
用法
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

备注

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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

IFormattable 成员
System 命名空间
IFormatProvider 接口
Object.ToString
CurrentCulture

其他资源

格式化概述