Published on

C# - 6.0 Interpolated Strings

Before C# 6

static void Main ( string [ ] args )
{
    var h = DateTime.Now.Hour;
    var m = DateTime.Now.Minute;
    Console.WriteLine ( "Now is {0}:{1}" , h , m );
    //Now is 2:12
}

C# 6

static void Main ( string [ ] args )
{
    var h = DateTime.Now.Hour;
    var m = DateTime.Now.Minute;
    Console.WriteLine ( $"Now is {h}:{m}" );
    //Now is 2:12
}

//不足兩位數時前方將自動補 0
static void Main ( string [ ] args )
{
    var h = DateTime.Now.Hour;
    var m = DateTime.Now.Minute;
    Console.WriteLine ( $"Now is {h:00}:{m:00}" );
    //Now is 02:12
}