C# 指南學習筆記(1)-HelloWorld,字串內插
阿新 • • 發佈:2018-12-13
使用 Visual Studio 2017 生成 C# .NET Core Hello World 應用程式:https://docs.microsoft.com/zh-cn/dotnet/core/tutorials/with-visual-studio
using System; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine("\nWhat is your name"); var name = Console.ReadLine(); var date = DateTime.Now; Console.WriteLine($"\nHello,{name},on {date:d} at {date:t}!"); Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date); Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now."); Console.Write("Press any key to continue..."); Console.ReadKey(true); } } }
- ReadLine()後面需要加(),DateTime.Now後面不加。
- 字串內插符,用於將{}內的內插字串解析為結果字串,$後面需緊跟",不然會編譯錯誤。比複合格式字串更為直觀。
- {date:HH:mm} 將date的值設定為HH:mm格式