C#中的字符串拼接@,$

转载自:https://blog.csdn.net/qq_40666620/article/details/101695138

一:@
@的意思是以@标注的字符出,其中所有的符号均为字符串符号,没有什么特殊字符,如’'什么的,均默认为字符串

static void Main(string[] args)
        {
        //这样就很方便写读取的文件路径,也不需要什么转义字符了
             StreamReader sr = new StreamReader(@"C:\Users\WANGWI8\Desktop\testStreamRead.txt");
             string line;
             while( (line = sr.ReadLine()) != null )
             {
                 Console.WriteLine(line);
             }
         }

二:$
$的意思是以它标注的字符串中,可用中括号取值:

  static void Main(string[] args)
        {
            var name = Console.ReadLine();
            var date = DateTime.Now;
            var result = $"hello {name} , you are my father , i am your son , writed at {date}";
            Console.WriteLine(result);

            var item2 =
            (
                name: "william",
                sex: 'f'
            );
            Console.WriteLine($"this is item : item.name : {item2.name} , this is sex : {item2.sex}");


            var inventory = new Dictionary<string, int>()
            {
                ["hammer, ball pein"] = 18,
                ["hammer, cross pein"] = 5,
                ["screwdriver, Phillips #2"] = 14
            };

            Console.WriteLine($"inventory on {DateTime.Now:d}");
            Console.WriteLine(" ");
            //负号的意思是左对齐,一共占据25个字符的空间
            Console.WriteLine($"|{"item",-25}|{"quantity",10}|");
            foreach (var item in inventory)
                Console.WriteLine($"|{item.Key,-25}|{item.Value,10}|");

        }

 

posted @ 2021-10-26 10:26  NewBBBB  阅读(1623)  评论(0编辑  收藏  举报