Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 662
Taegos Avatar asked Dec 18 '22 20:12

Taegos


2 Answers

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}");
like image 112
Christos Avatar answered Dec 28 '22 22:12

Christos


You can use a string format:

Console.WriteLine("**** Name of product: {0}", productName);
like image 30
Thomas Avatar answered Dec 29 '22 00:12

Thomas