How to use C#quotes?

Asked

Viewed 120 times

5

I’m starting to program in C# and I’m feeling some difficulty with quotation marks when I need to paste some text. In Python I could use the triple quotes to paste some text that there was line break, already in C# I have no idea how to do this and I end up having to do manually with \n

Python example:

print('''Olá!
Como você está?''')
  • 1

    If you can post an example of code, it would help to understand the situation. But in advance, try to use the property Environment.NewLine

2 answers

9


The solution that is equivalent to Python triple quotes is @. It is possible to do it in other ways, but they are not equivalent. Only the @, called string Verbatim has the same benefits and commitments you get with Python.

Concatenation in some cases may be optimized and end up giving in it, but do not count that it will always occur the same. The StringBuilder is for another kind of need.

Do so:

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

In case you need to put something inside her you can use the interpolation:

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á?");
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • 1

    I use a lot with $, makes it too easy with variables, than staying put many +.

4

Here come some forms:

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);
}

Exit:

//Olá 1!
//Como você está?
//Olá 2!
//Como você está?
//Olá 3! 
//Como você está ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.