How to print a value in console application C#

13,921

Solution 1

Change

To

 Console.WriteLine("The password = "+ X);

or

Console.WriteLine("The password = {0}", X);

Solution 2

Replace

Console.WriteLine("The password = ", X);

With

Console.WriteLine("The password = {0}", X);

You are missing a placeholder for your variable. If you had more variables, it would be

 Console.WriteLine("The password = {0} and other value {1}", X, Y);

EDIT: You can also use

Console.WriteLine("The password = "+ X);
//or note the $ sign before string. It will render variables inside {} 
Console.WriteLine($"The password = {X}");
Share:
13,921
ispostback
Author by

ispostback

Started as .Net Developer, right now on the verge of being Frontend Developer. I am also in Quora Presently working on an Angular 5 application.

Updated on August 01, 2022

Comments

  • ispostback
    ispostback almost 2 years

    This is the code which running on my console.

    public static void Main(string[] args)
    {
        string X = GeneratePassword();
        Console.WriteLine("The password = ", X);
        Console.ReadLine();
        Console.ReadKey();
    }
    

    My GeneratePassword function is returning value for sure, but somehow I am unable to print it. I might be missing a small thing. Please help.

    Thanks in advance.

  • ispostback
    ispostback over 7 years
    I am returning a single string, why should I use your 3rd code block??
  • Robert
    Robert over 7 years
    @Arka_Dev I gave you an example why your code does not work, and provided you with more info how to use a solution if you had more than one variable.
  • ispostback
    ispostback over 7 years
    Sure, more of a generalistic approach.anyway thanks for mentioning
  • Robert
    Robert over 7 years
    np. I added more examples you could use :)