How can I show the value of a variable on a screen within a text in c #? [duplicate]

4

I'm doing a desktop application and I want to show the value of a variable on the screen inside the quotes this is my code

 static void Main(string[] args)
    {

        int casa = 16;


        Console.WriteLine("variable {{casa}}");
        Console.ReadKey();
    }

Print casa no 16 , it can be done adding and the name of the variable but I want it inside the quotes could it be done ???

    
asked by ortiga 19.07.2018 в 14:00
source

2 answers

4

There are two ways to show the value of the variable:

Placing the variable after the operator + , outside the ""

1) Console.WriteLine("variable" + casa);

Or this second option is available only for C # > = 6.0 . Place the symbol $ after the start parenthesis and placing the name of the variable between {} within the "" :

2 Console.WriteLine($"variable {casa}");

Greetings.

    
answered by 19.07.2018 / 14:16
source
2

Having several options, the two most common, would be,

Concatenate

 static void Main(string[] args)
    {

        int casa = 16;
        Console.WriteLine("variable "+ casa);
        //Mostraría variable 16
        Console.ReadKey();
    }

Or you could also build a string with String.Format , but I would apply, perhaps, to more specific situations

 static void Main(string[] args)
    {

        int casa = 16;
        String texto = String.Format("La variable es {0}.",
                     casa);
        Console.WriteLine(s);
        //Mostraría La variable es 16
        Console.ReadKey();
    }

As I said before, you have an infinite way of doing what you are looking for, it would be a matter of looking for which is the most comfortable for you.

Greetings

    
answered by 19.07.2018 в 14:08