Lire en anglais Modifier

Partager via


String/Character escape sequence \e

Notes

This article is a feature specification. The specification serves as the design document for the feature. It includes proposed specification changes, along with information needed during the design and development of the feature. These articles are published until the proposed spec changes are finalized and incorporated in the current ECMA specification.

There may be some discrepancies between the feature specification and the completed implementation. Those differences are captured in the pertinent language design meeting (LDM) notes.

You can learn more about the process for adopting feature speclets into the C# language standard in the article on the specifications.

Champion issue: https://github.com/dotnet/csharplang/issues/8657

Summary

An addition of the string/character escape sequence \e as a shortcut/short-hand replacement for the character code point 0x1b, commonly known as the ESCAPE (or ESC) character.
This character is currently accessible using one of the following escape sequences:

  • \u001b
  • \U0000001b
  • \x1b (not recommended, see the picture attached at the bottom.)

With the implementation of this proposal, the following assertions should be true:

C#
char escape_char = '\e';

Assert.IsTrue(escape_char == (char)0x1b, "...");
Assert.IsTrue(escape_char == '\u001b', "...");
Assert.IsTrue(escape_char == '\U0000001b', "...");
Assert.IsTrue(escape_char == '\x1b', "...");

Detailed design

The language syntax specification is changed as follows in section 6.4.5.5:

diff
fragment Simple_Escape_Sequence
-    : '\\\'' | '\\"' | '\\\\' | '\\0' | '\\a' | '\\b' | '\\f' | '\\n' | '\\r' | '\\t' | '\\v'
+    : '\\\'' | '\\"' | '\\\\' | '\\0' | '\\a' | '\\b' | '\\f' | '\\n' | '\\r' | '\\t' | '\\v' | '\\e'
    ;

As well as the addition of the last line to the following table in the specifications:

A simple escape sequence represents a Unicode character, as described in the table below.

Escape sequence Character name Unicode code point
\' Single quote U+0027
... ... ...
\e Escape character U+001B

The type of a Character_Literal is char.