问题
A simple question: How do you display strings in the CMD using Console.Writeline() using C# in VS? I Know you use + for ints and floats. But what do you use for strings? This is what i have:
private string productName;
public void GetItemData()
{
ShowReciept();
}
private void ReadItem()
{
Console.WriteLine("Enter the product's name: ");
productName = Console.ReadLine();
}
private void ShowReciept()
{
Console.WriteLine("**** Name of product:", productName);
}
In void ShowReciept() it writes out everything in the Console.WriteLine command, exept the product Name. So its just blank were the product name should have been.
回答1:
You can use string concatenation:
Console.WriteLine("**** Name of product:" + productName);
or you can use this:
Console.WriteLine("**** Name of product:{0}", productName);
Furthermore, if you program in C# 6, you can use string interpolation:
Console.WriteLine($"**** Name of product:{productName}");
回答2:
You can use a string format:
Console.WriteLine("**** Name of product: {0}", productName);
来源:https://stackoverflow.com/questions/34908837/console-writeline-using-strings