格式化字符串的几种方式
发布日期:2021-07-01 04:22:17 浏览次数:43 分类:技术文章

本文共 498 字,大约阅读时间需要 1 分钟。

文章目录

1 使用占位符

简单记一下,这种方式输出不容易出错。

static void Main(string[] args){
string stuName = "张晓晓"; int stuAge = 20; //使用字符串拼接方法输出 Console.WriteLine("学员姓名:" + stuName + "年龄:" + stuAge); //使用占位符方式输出(格式化输出) Console.WriteLine("学员姓名:{0} 年龄:{1}", stuName, stuAge); Console.ReadLine();}

2 使用4.6中的语法糖

static void Main(string[] args){
string stuName = "张晓晓"; int stuAge = 20; //使用.NET 4.6引入的语法糖 Console.WriteLine($"学员姓名:{stuName} 年龄:{stuAge}"); Console.ReadLine();}

参考资料:

转载地址:https://muzimin.blog.csdn.net/article/details/102752990 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:.NET平台相关概念(简单了解)
下一篇:预处理指令的开始和结束

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月15日 21时39分47秒