$ - string interpolation (C# reference)

Console Programming/C# Console 2019. 9. 20. 11:39

https://docs.microsoft.com/ko-kr/dotnet/csharp/language-reference/tokens/interpolated

 

$ - 문자열 보간 - C# 참조

문자열 보간을 이용한 구문으로 기존의 문자열 합성보다 읽기 쉽고 편리하게 문자열 출력의 서식을 지정할 수 있습니다.

docs.microsoft.com

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Syntax05
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "Mark";
            var date = DateTime.Now;
 
            // Composite formatting:
            Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
           
            // String interpolation:
            Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
            
            // Both calls produce the same output that is similar to:
            // Hello, Mark! Today is Wednesday, it's 19:40 now.
        }
    }
    
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

 

Composite formatting 과 String interpolation 두가지 표현 방식이 있다.

 

Composite formatting은

Console.WriteLine(" {0} {1} ", var1, var2);

0에  var1 값이 1에 var2값이 들어가서 출력되고,

 

String interpolation은

Console.WriteLine($" {var1} {var2} ");

변수를 { }를 사용해 곧바로 " "안에 써넣는 것이 가능하다.

 

 

두 가지 방식 모두 출력결과는 같다.

: