IFormatProvider.GetFormat Method (Type)


Returns an object that provides formatting services for the specified type.

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

object GetFormat(
	Type formatType


Type: System.Type

An object that specifies the type of format object to return.

Return Value

Type: System.Object

An instance of the object specified by formatType, if the IFormatProvider implementation can supply that type of object; otherwise, null.

GetFormat is a callback method that formatting and parsing methods invoke to retrieve information about the format of the input string in parsing operations or the format of the output string in formatting operations. In the formatType parameter, the formatting or parsing method passes the type of object it requires to perform its operation. If the IFormatProvider implementation can supply this formatting or parsing object, it returns that object. If not, it returns null.

For example, in the call to the Int32.ToString(IFormatProvider) method, the method argument is an IFormatProvider object that provides information about how the string representation of the current integer instance might be formatted. When the runtime executes the method, it calls the IFormatProvider object's GetFormat method and passes it a Type object that represents the NumberFormatInfo type. If the IFormatProvider object can supply a NumberFormatInfo object, it returns that object. If it cannot supply an object of that type, it returns null.

You can implement the IFormatProvider interface and the GetFormat method in classes that provide custom formatting or parsing services. The IFormatProvider implementation is then passed as an argument to any overload of a parsing or formatting method that has a parameter of type IFormatProvider, such as String.Format(IFormatProvider, String, Object[]), Int32.ToString(String, IFormatProvider), or DateTime.Parse(String, IFormatProvider).

The following example illustrates the use of a class that implements the IFormatProvider interface and the GetFormat method. The AcctNumberFormat class converts an Int64 value that represents an account number to a formatted 12-digit account number. Its GetFormat method returns a reference to itself if the formatType parameter refers to a class that implements ICustomFormatter; otherwise, GetFormat returns null.

using System;
using System.IO;
using System.Text.RegularExpressions;

public class WordCount
   private String filename = String.Empty;
   private int nWords = 0;
   private String pattern = @"\b\w+\b"; 

   public WordCount(string filename)
      if (! File.Exists(filename))
         throw new FileNotFoundException("The file does not exist.");

      this.filename = filename;
      string txt = String.Empty;
      StreamReader sr = null;
      try {
         sr = new StreamReader(filename);
         txt = sr.ReadToEnd();
      finally {
         if (sr != null) sr.Dispose();     
      nWords = Regex.Matches(txt, pattern).Count;

   public string FullName
   { get { return filename; } }

   public string Name
   { get { return Path.GetFileName(filename); } }

   public int Count 
   { get { return nWords; } }

An instance of the AcctNumberFormat class can then be passed as an argument to a method that provides formatting or parsing services. For example, the following code passes an AcctNumberFormat class to the String.Format(IFormatProvider, String, Object[]) method to generate a formatted 12-digit account number.

using System;
using System.Globalization;

public enum DaysOfWeek { Monday=1, Tuesday=2 };

public class TestFormatting
   public static void Main()
      long acctNumber;
      double balance; 
      DaysOfWeek wday; 
      string output;

      acctNumber = 104254567890;
      balance = 16.34;
      wday = DaysOfWeek.Monday;

      output = String.Format(new AcctNumberFormat(), 
                             "On {2}, the balance of account {0:H} was {1:C2}.", 
                             acctNumber, balance, wday);

      wday = DaysOfWeek.Tuesday;
      output = String.Format(new AcctNumberFormat(), 
                             "On {2}, the balance of account {0:I} was {1:C2}.", 
                             acctNumber, balance, wday);
// The example displays the following output:
//       On Monday, the balance of account 10425-456-7890 was $16.34.
//       On Tuesday, the balance of account 104254567890 was $16.34.

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft