Console.writeline using strings

社会主义新天地 提交于 2021-02-07 08:46:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!