C# string interpolation with variable format

后端 未结 6 1567
时光取名叫无心
时光取名叫无心 2021-01-18 06:15

I need to format a variable with string interpolation, and the format string is another variable:

here is my sample code:

static void Main(string[] a         


        
6条回答
  •  盖世英雄少女心
    2021-01-18 06:55

    Your code is equivalent to:

    Console.WriteLine(String.Format("Test 2: {0:formatString}", i));
    

    As the formatString is in the format string, you would nest String.Format calls to put the value in the format string:

    Console.WriteLine(String.Format(String.Format("Test 2: {{0:{0}}}", formatstring), i));
    

    This isn't supported with string interpolation.

提交回复
热议问题