IFormattable インターフェイス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
オブジェクトの値を文字列形式で書式設定する機能を提供します。
public interface class IFormattable
	public interface IFormattable
	[System.Runtime.InteropServices.ComVisible(true)]
public interface IFormattable
	type IFormattable = interface
	[<System.Runtime.InteropServices.ComVisible(true)>]
type IFormattable = interface
	Public Interface IFormattable
	- 派生
 
- 属性
 
例
次の例では、 Temperature インターフェイスを実装する IFormattable クラスを定義しています。 クラスは、"G" と "C" の 4 つの書式指定子をサポートしています。これは、温度が摂氏で表示されることを示します。"F", これは、温度が華氏で表示されることを示します;と "K" は、温度がケルビンに表示されることを示します。 さらに、実装では IFormattable.ToString 、 null または空の書式指定文字列を処理することもできます。 クラスによって定義されているTemperature他の 2 つのToStringメソッドは、実装のIFormattable.ToString呼び出しをラップするだけです。
using System;
using System.Globalization;
public class Temperature : IFormattable
{
   private decimal temp;
   public Temperature(decimal temperature)
   {
      if (temperature < -273.15m)
        throw new ArgumentOutOfRangeException(String.Format("{0} is less than absolute zero.",
                                              temperature));
      this.temp = temperature;
   }
   public decimal Celsius
   {
      get { return temp; }
   }
   public decimal Fahrenheit
   {
      get { return temp * 9 / 5 + 32; }
   }
   public decimal Kelvin
   {
      get { return temp + 273.15m; }
   }
   public override string ToString()
   {
      return this.ToString("G", CultureInfo.CurrentCulture);
   }
   public string ToString(string format)
   {
      return this.ToString(format, CultureInfo.CurrentCulture);
   }
   public string ToString(string format, IFormatProvider provider)
   {
      if (String.IsNullOrEmpty(format)) format = "G";
      if (provider == null) provider = CultureInfo.CurrentCulture;
      switch (format.ToUpperInvariant())
      {
         case "G":
         case "C":
            return temp.ToString("F2", provider) + " °C";
         case "F":
            return Fahrenheit.ToString("F2", provider) + " °F";
         case "K":
            return Kelvin.ToString("F2", provider) + " K";
         default:
            throw new FormatException(String.Format("The {0} format string is not supported.", format));
      }
   }
}
open System
open System.Globalization
type Temperature(temperature: decimal) =
    do 
        if temperature < -273.15M then
            raise (ArgumentOutOfRangeException $"{temperature} is less than absolute zero.")
    member _.Celsius =
        temperature
    member _.Fahrenheit =
        temperature * 9M / 5M + 32M
    member _.Kelvin =
        temperature + 273.15m
    override this.ToString() =
        this.ToString("G", CultureInfo.CurrentCulture)
    member this.ToString(format) =
        this.ToString(format, CultureInfo.CurrentCulture)
    member this.ToString(format, provider: IFormatProvider) =
        let format =
            if String.IsNullOrEmpty format then "G"
            else format
        let provider =
            if isNull provider then 
                CultureInfo.CurrentCulture :> IFormatProvider
            else provider
        match format.ToUpperInvariant() with
        | "G" | "C" ->
            temperature.ToString("F2", provider) + " °C"
        | "F" ->
            this.Fahrenheit.ToString("F2", provider) + " °F"
        | "K" ->
            this.Kelvin.ToString("F2", provider) + " K"
        | _ ->
            raise (FormatException $"The {format} format string is not supported.")
    interface IFormattable with
        member this.ToString(format, provider) = this.ToString(format, provider)
Imports System.Globalization
Public Class Temperature : Implements IFormattable
   Private temp As Decimal
   
   Public Sub New(temperature As Decimal)
      If temperature < -273.15 Then _ 
        Throw New ArgumentOutOfRangeException(String.Format("{0} is less than absolute zero.", _
                                              temperature))
      Me.temp = temperature
   End Sub
   
   Public ReadOnly Property Celsius As Decimal
      Get
         Return temp
      End Get
   End Property
   
   Public ReadOnly Property Fahrenheit As Decimal
      Get
         Return temp * 9 / 5 + 32
      End Get
   End Property
   
   Public ReadOnly Property Kelvin As Decimal
      Get
         Return temp + 273.15d
      End Get
   End Property
   Public Overrides Function ToString() As String
      Return Me.ToString("G", CultureInfo.CurrentCulture)
   End Function
      
   Public Overloads Function ToString(fmt As String) As String
      Return Me.ToString(fmt, CultureInfo.CurrentCulture)
   End Function
   
   Public Overloads Function ToString(fmt As String, provider As IFormatProvider) _
                   As String _
                   Implements IFormattable.ToString
      If String.IsNullOrEmpty(fmt) Then fmt = "G"
      If provider Is Nothing Then provider = CultureInfo.CurrentCulture
      
      Select Case fmt.ToUpperInvariant()
         Case "G", "C"
            Return temp.ToString("F2", provider) + " °C" 
         Case "F"
            Return Fahrenheit.ToString("F2", provider) + " °F"
         Case "K"
            Return Kelvin.ToString("F2", provider) + " K"
         Case Else
            Throw New FormatException(String.Format("The {0} format string is not supported.", fmt))
      End Select
   End Function
End Class
次の例では、実装を IFormattable.ToString 直接呼び出すか、複合書式指定文字列を使用して呼び出します。
public class Example
{
   public static void Main()
   {
      // Use composite formatting with format string in the format item.
      Temperature temp1 = new Temperature(0);
      Console.WriteLine("{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)\n", temp1);
      // Use composite formatting with a format provider.
      temp1 = new Temperature(-40);
      Console.WriteLine(String.Format(CultureInfo.CurrentCulture, "{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)", temp1));
      Console.WriteLine(String.Format(new CultureInfo("fr-FR"), "{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)\n", temp1));
      // Call ToString method with format string.
      temp1 = new Temperature(32);
      Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)\n",
                        temp1.ToString("C"), temp1.ToString("K"), temp1.ToString("F"));
      // Call ToString with format string and format provider
      temp1 = new Temperature(100)      ;
      NumberFormatInfo current = NumberFormatInfo.CurrentInfo;
      CultureInfo nl = new CultureInfo("nl-NL");
      Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)",
                        temp1.ToString("C", current), temp1.ToString("K", current), temp1.ToString("F", current));
      Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)",
                        temp1.ToString("C", nl), temp1.ToString("K", nl), temp1.ToString("F", nl));
   }
}
// The example displays the following output:
//    0.00 °C (Celsius) = 273.15 K (Kelvin) = 32.00 °F (Fahrenheit)
//
//    -40.00 °C (Celsius) = 233.15 K (Kelvin) = -40.00 °F (Fahrenheit)
//    -40,00 °C (Celsius) = 233,15 K (Kelvin) = -40,00 °F (Fahrenheit)
//
//    32.00 °C (Celsius) = 305.15 K (Kelvin) = 89.60 °F (Fahrenheit)
//
//    100.00 °C (Celsius) = 373.15 K (Kelvin) = 212.00 °F (Fahrenheit)
//    100,00 °C (Celsius) = 373,15 K (Kelvin) = 212,00 °F (Fahrenheit)
open System
open System.Globalization
[<EntryPoint>]
let main _ =
    // Use composite formatting with format string in the format item.
    let temp1 = Temperature 0
    Console.WriteLine("{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)\n", temp1)
    // Use composite formatting with a format provider.
    let temp1 = Temperature -40
    String.Format(CultureInfo.CurrentCulture, "{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)", temp1)
    |> printfn "%s"
    String.Format(CultureInfo "fr-FR", "{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)\n", temp1)
    |> printfn "%s"
    // Call ToString method with format string.
    let temp1 = Temperature 32
    Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)\n",
                      temp1.ToString "C", temp1.ToString "K", temp1.ToString "F")
    // Call ToString with format string and format provider
    let temp1 = Temperature 100      
    let current = NumberFormatInfo.CurrentInfo
    let nl = CultureInfo "nl-NL"
    Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)",
                      temp1.ToString("C", current), temp1.ToString("K", current), temp1.ToString("F", current))
    Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)",
                      temp1.ToString("C", nl), temp1.ToString("K", nl), temp1.ToString("F", nl))
    0
// The example displays the following output:
//    0.00 °C (Celsius) = 273.15 K (Kelvin) = 32.00 °F (Fahrenheit)
//
//    -40.00 °C (Celsius) = 233.15 K (Kelvin) = -40.00 °F (Fahrenheit)
//    -40,00 °C (Celsius) = 233,15 K (Kelvin) = -40,00 °F (Fahrenheit)
//
//    32.00 °C (Celsius) = 305.15 K (Kelvin) = 89.60 °F (Fahrenheit)
//
//    100.00 °C (Celsius) = 373.15 K (Kelvin) = 212.00 °F (Fahrenheit)
//    100,00 °C (Celsius) = 373,15 K (Kelvin) = 212,00 °F (Fahrenheit)
Module Example
   Public Sub Main()
      ' Use composite formatting with format string in the format item.
      Dim temp1 As New Temperature(0)
      Console.WriteLine("{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)", temp1)
      Console.WriteLine()
      
      ' Use composite formatting with a format provider.
      temp1 = New Temperature(-40)
      Console.WriteLine(String.Format(CultureInfo.CurrentCulture, "{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)", temp1))
      Console.WriteLine(String.Format(New CultureInfo("fr-FR"), "{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)", temp1))
      Console.WriteLine()
      
      ' Call ToString method with format string.
      temp1 = New Temperature(32)
      Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)", _
                        temp1.ToString("C"), temp1.ToString("K"), temp1.ToString("F"))
      Console.WriteLine()
      ' Call ToString with format string and format provider
      temp1 = New Temperature(100)      
      Dim current As NumberFormatInfo = NumberFormatInfo.CurrentInfo
      Dim nl As New CultureInfo("nl-NL") 
      Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)", _
                        temp1.ToString("C", current), temp1.ToString("K", current), temp1.ToString("F", current))
      Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)", _
                        temp1.ToString("C", nl), temp1.ToString("K", nl), temp1.ToString("F", nl))
   End Sub
End Module
' The example displays the following output:
'       0.00 °C (Celsius) = 273.15 K (Kelvin) = 32.00 °F (Fahrenheit)
'       
'       -40.00 °C (Celsius) = 233.15 K (Kelvin) = -40.00 °F (Fahrenheit)
'       -40,00 °C (Celsius) = 233,15 K (Kelvin) = -40,00 °F (Fahrenheit)
'       
'       32.00 °C (Celsius) = 305.15 K (Kelvin) = 89.60 °F (Fahrenheit)
'       
'       100.00 °C (Celsius) = 373.15 K (Kelvin) = 212.00 °F (Fahrenheit)
'       100,00 °C (Celsius) = 373,15 K (Kelvin) = 212,00 °F (Fahrenheit)
	注釈
インターフェイスは IFormattable 、書式指定文字列と書式プロバイダーに基づいて、オブジェクトを文字列形式に変換します。
書式指定文字列は、通常、オブジェクトの一般的な外観を定義します。 たとえば、.NET Frameworkでは次の機能がサポートされています。
列挙値を書式設定するための標準書式指定文字列 (「 列挙型書式指定文字列」を参照)。
数値を書式設定するための標準書式指定文字列とカスタム書式 指定文字列 (「標準の数値書式指定文字列 」および「 カスタム数値書式指定文字列」を参照)。
日付と時刻の値を書式設定するための標準書式指定文字列とカスタム書式 指定文字列 (「標準の日付と時刻の書式指定文字列 」および「 カスタム日付と時刻の書式指定文字列」を参照してください)。
時間間隔を書式設定するための標準書式指定文字列とカスタム書式指定文字列 ( 「標準 TimeSpan 書式指定文字列 」および「 カスタム TimeSpan 書式指定文字列」を参照)。
独自の書式指定文字列を定義して、アプリケーション定義型の書式設定をサポートすることもできます。
書式プロバイダーは、通常、オブジェクトを文字列表現に変換する際に使用されるシンボルを定義する書式設定オブジェクトを返します。 たとえば、数値を通貨値に変換する場合、書式プロバイダーは結果文字列に表示される通貨記号を定義します。 .NET Frameworkでは、次の 3 つの形式プロバイダーが定義されています。
System.Globalization.CultureInfo数値を書式設定するためのオブジェクト、NumberFormatInfoまたはDateTimeFormatInfo日付と時刻の値を書式設定するためのオブジェクトを返す クラス。
クラス。 System.Globalization.NumberFormatInfo これは、数値を書式設定するための それ自体のインスタンスを返します。
System.Globalization.DateTimeFormatInfo日付と時刻の値を書式設定するための それ自体のインスタンスを返す クラス。
さらに、独自のカスタム書式プロバイダーを定義して、書式設定で使用されるカルチャ固有、職業固有、または業界固有の情報を提供できます。 カスタム書式プロバイダーを使用したカスタム書式の実装の詳細については、「」を参照してください ICustomFormatter。
インターフェイスは IFormattable 、 ToString実装する型の書式設定サービスを提供する 1 つのメソッド を定義します。 メソッドは ToString 直接呼び出すことができます。 さらに、 メソッドと Convert.ToString(Object, IFormatProvider) メソッド、および .NET Frameworkの複合書式設定機能を使用するメソッドによって自動的Convert.ToString(Object)に呼び出されます。 このようなメソッドには Console.WriteLine(String, Object)、、 String.Format、、 StringBuilder.AppendFormat(String, Object)などがあります。 メソッドは ToString 、メソッドの書式指定文字列内の書式指定項目ごとに呼び出されます。
インターフェイスは IFormattable 、基本データ型によって実装されます。
注意 (実装者)
が提供するよりも ToString() 文字列の書式設定をより詳細に制御する必要があるクラスでは、 を実装 IFormattableする必要があります。
を実装 IFormattable するクラスは、"G" (一般) 書式指定子をサポートする必要があります。 クラスは、"G" 指定子に加えて、サポートされている書式指定子の一覧を定義できます。 さらに、 の書式指定子を処理するには、 クラスを準備する null必要があります。 書式設定と書式設定コードの詳細については、「書式の種類」を参照してください。
メソッド
| ToString(String, IFormatProvider) | 
		 指定された書式を使用して現在のインスタンスの値を書式設定します。  |