What does (Char)0 / (Char)8 / (Char)13 mean?

I'm doing a shopping program and found na on that same site. The answer to a problem I had, but did not understand what the (Char)0 part would be, among others. I need an explanation of what this is.

private void preVenda_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((e.KeyChar < '0' || e.KeyChar > '9') && 
         e.KeyChar != ',' && e.KeyChar != '.' && 
         e.KeyChar != (char)13 && e.KeyChar != (char)8)
    {
        e.KeyChar = (char)0;
    }
    else
    {
        if (e.KeyChar == '.' || e.KeyChar == ',')
        {
            if (!preVenda.Text.Contains(','))
            {
                e.KeyChar = ',';
            }
            else
            {
                e.KeyChar = (char)0;
            }
        }
    }
}

Follows the picture:

Code

Author: Maniero, 2018-11-25

1 answers

And do you understand that?

'\0', '\8', '\13'

Is the same thing. Everything is number on the computer. The type char is just something that conceptually is treated with "letters" (characters) and when you have it shown it is already drawn as text, but the encoding of each character is a number following a table. C# uses UTF-16 encoding by default, but the bulk of the characters we use are from the ASCII table , so each number refers to one character, and not all are printable, some are do some specific action or determine something that is not printable, which is the case for everyone below code 32.

0 is null, 8 is a tab, and 13 is a line change. The backslash is an escape character, in the syntax of most languages, including C#, it indicates that what comes next something special and should not be treated as a character but as a code of the encoding table or even some special term, for example 8 can be expressed as \t and 13 as \r, or in some cases as \n, but then it depends on platform. It is necessary to do so precisely because they are not printable.

Since C # is a strong typing language it avoids mixing types and making automatic promotions that can give some problem, so you can't assign numbers or use as operand of char directly, you have to explicitly guarantee that that the type being used is a char, so it is made a cast in the number just to state to the compiler that you know what you're doing and that number should be interpreted as a character.

 2
Author: Maniero, 2020-08-18 15:40:19