CSharp - Write program to Outputting Special Characters

Requirements

To output a special character to the screen.

Here are some examples:

  • Output Enter to terminate a line.
  • Output a quote mark.
  • Output a Unicode character.

Hint

To work with special characters, you use escape sequences.

In C#, an escape sequence starts with a backslash.

Demo

using System;

class Program//from  ww w.j  a v a 2s . c  o m
{
    static void Main(string[] args)
    {
        // Multiline output 
        Console.WriteLine("First line\r\nSecond line");

        // specifying "Enter" in more human form 
        Console.WriteLine("First line" + Environment.NewLine + "Second line");

        // Text containing a quote 
        Console.WriteLine("The letter started so sweet: \"this is a test\"");

        // Unicode characters, in this case Greek beta 
        Console.WriteLine("If the font knows, here is Greek beta: \u03B2");

        // Backslashes themselves need to be doubled 
        Console.WriteLine("Path to desktop on my computer: " + "C:\\Users\\test\\Desktop"); 

    }
}

Result

Note

In C#, a backslash in text introduces an escape sequence.

To output a backslash, double it. This is often the case when dealing with file paths in the Windows operating system.

Console.WriteLine will recognize \n as a line terminator.

"the whole Enter" is signified with \r\n.

You can also use Environment.NewLine.