Search code examples
c#stringvisual-studioconsole.writeline

Console.writeline using strings


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.


Solution

  • 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}");