Passing value inside Console.WriteLine

Pரதீப் picture Pரதீப் · May 29, 2017 · Viewed 7.2k times · Source

Here is a piece of code am working on

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter the number to find factorial : ");
        var fac = Convert.ToInt32(Console.ReadLine());
        var res = 1;
        for (var i = fac; i >= 1; i--)
        {
            res = res * i;
        }
        Console.WriteLine("Count of " + fac + " is : " + res);
    }
}

I want to add the user entered input fac in result. Here am using Console.WriteLine("Count of " + fac + " is : " + res); to display the output in my console.

I tried something like

Console.WriteLine("Count of {0} is : {1} ", fac, res);

Is there a better way to do this or this is just fine..

Answer

Ousmane D. picture Ousmane D. · May 29, 2017

Another solution would be to use string interpolation:

Console.WriteLine($"Count of {fac} is : {res}");