CSharp - Composite formatting String

Introduction

Composite format strings combines variable substitution with format strings.

static string.Format method accepts a composite format string.

Demo

using System;
class MainClass/*w w w. j a  v a2  s .c o  m*/
{
   public static void Main(string[] args)
   {
      string composite = "Credit={0:C}";
      Console.WriteLine (string.Format (composite, 500));   // Credit=$500.00
   }
}

Result

Console class overloads its Write and WriteLine methods to accept composite format strings.

Console.WriteLine ("Credit={0:C}", 500);   // Credit=$500.00

You can append a composite format string to a StringBuilder via AppendFormat, and to a TextWriter for I/O.

string.Format accepts an optional format provider.

You can call ToString on an object while passing in a format provider. For example:

string s = string.Format (CultureInfo.InvariantCulture, "{0}", someObject);