How to use C # quotes?

4

I'm starting to program in C # and I'm having some difficulties with the quotation marks when I need to paste some text. In Python I could use the triple quotation marks to paste some text that there was a line break, already in C # I have no idea how to do this and I just have to do it manually with \n

Python Example:

print('''Olá!
Como você está?''')
    
asked by anonymous 15.11.2018 / 13:43

2 answers

8

The solution that is equivalent to the triple quotation marks of Python is @ . It is possible to do it in other ways, but they are not equivalent. Only @ , called string verbatim has the same benefits and commitments as gets with Python.

The concatenation in some cases can have optimization and end up giving the same, but do not count that will always occur the same. StringBuilder is for another type of need.

Do this:

WriteLine(@"Olá!
Como você está?");

In cases where you need to put something inside it you can use tweening :

using static System.Console;

public class Program {
    public static void Main() {
        var nome = "João";
        WriteLine(@"Olá!
Como você está?");
        WriteLine($@"Olá {nome}!
Como você está?");
    }
}

See running on .NET Fiddle . And no Coding Ground . Also I put it in GitHub for future reference .

    
15.11.2018 / 14:43
3

Here are some ways:

public static void Main()
{
    Console.WriteLine(@"Olá 1!
Como você está?");

    Console.WriteLine("Olá 2!\n"+"Como você está?");

    Console.WriteLine("Olá 3! {0}Como você está ?",
                      Environment.NewLine);
}

Output:

//Olá 1!
//Como você está?
//Olá 2!
//Como você está?
//Olá 3! 
//Como você está ?
    
15.11.2018 / 14:10