New in .NET 9.0(6): New escape character for console output

0
15
New in .NET 9.0(6): New escape character for console output


with the elders VT100/ANSI escape code You can still trigger a variety of formatting in console applications today, including 24-bit color, boldface, underline, strikethrough, and flashing. VT100/ANSI codes are introduced by the escape character (ASCII character 27, hexadecimal: 0x1b).

Advertisement





Dr. Holger Schweitenberg is the technical director of the expert network www.IT-Visions.de, which supports many medium-sized and large companies through consulting and training as well as software development with 53 renowned experts. Through his attendance at numerous national and international conferences as well as more than 90 expert books and more than 1,500 expert articles, Holger Schwittenberg is one of the best-known experts for .NET and web technologies in Germany.

New in .NET 9.0(6): New escape character for console outputNew Escape Sign for Console Editions in .NET 9.0 (6)

Before C# 13.0, you could escape this ASCII character 27 in .NET console applications Console.WriteLine() just express strangely \u001b, \U0000001b Or \x1b, Although the latter is not recommended: “If you use the escape sequence \x, specify less than four hexadecimal digits, and the characters immediately following the escape sequence are valid hexadecimal digits (for example 0-9, A-F, and A-F ), these are interpreted as part of the escape. For example, \xA1 produces “¡” (matches the code point U+00A1), but if the next character is “A” or “a”. is instead interpreted as \xA1A and generates the code point “च” (matches the code point U+0A1A), in such cases च is a Punjabi character and the language spoken in Pakistan and India. ,Misinterpretations can be avoided by specifying all four hexadecimal digits (e.g. \x00A1).”

Typically, before C# 13.0 the output looked like this with VT100/ANSI escape codes:

Console.WriteLine("This is a regular text");
Console.WriteLine("\u001b(1mThis is a bold text\u001b(0m");
Console.WriteLine("\u001b(2mThis is a dimmed text\u001b(0m");
Console.WriteLine("\u001b(3mThis is an italic text\u001b(0m");
Console.WriteLine("\u001b(4mThis is an underlined text\u001b(0m");
Console.WriteLine("\u001b(5mThis is a blinking text\u001b(0m");
Console.WriteLine("\u001b(6mThis is a fast blinking text\u001b(0m");
Console.WriteLine("\u001b(7mThis is an inverted text\u001b(0m");
Console.WriteLine("\u001b(8mThis is a hidden text\u001b(0m");
Console.WriteLine("\u001b(9mThis is a crossed-out text\u001b(0m");
Console.WriteLine("\u001b(21mThis is a double-underlined text\u001b(0m");
Console.WriteLine("\u001b(38;2;255;0;0mThis is a red text\u001b(0m");
Console.WriteLine("\u001b(48;2;255;0;0mThis is a red background\u001b(0m");
Console.WriteLine("\u001b(38;2;0;0;255;48;2;255;255;0mThis is a blue text with a yellow background\u001b(0m");

Since C# 13.0 is now \e Escape character as a short form of ASCII 27, so that character strings become more compact and clear:

Console.WriteLine("This is a regular text");
Console.WriteLine("\e(1mThis is a bold text\e(0m");
Console.WriteLine("\e(2mThis is a dimmed text\e(0m");
Console.WriteLine("\e(3mThis is an italic text\e(0m");
Console.WriteLine("\e(4mThis is an underlined text\e(0m");
Console.WriteLine("\e(5mThis is a blinking text\e(0m");
Console.WriteLine("\e(6mThis is a fast blinking text\e(0m");
Console.WriteLine("\e(7mThis is an inverted text\e(0m");
Console.WriteLine("\e(8mThis is a hidden text\e(0m");
Console.WriteLine("\e(9mThis is a crossed-out text\e(0m");
Console.WriteLine("\e(21mThis is a double-underlined text\e(0m");
Console.WriteLine("\e(38;2;255;0;0mThis is a red text\e(0m");
Console.WriteLine("\e(48;2;255;0;0mThis is a red background\e(0m");
Console.WriteLine("\e(38;2;0;0;255;48;2;255;255;0mThis is a blue text with a yellow background\e(0m");



The output of the previous two listings looks identical.

(Image: Screenshot (Holger Schweitenberg))


(rme)

Mobile Development: Android 16 apps ban on presentationMobile Development: Android 16 apps ban on presentation

LEAVE A REPLY

Please enter your comment!
Please enter your name here