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

Asked

Viewed 821 times

1

I’m doing a shopping program and found on this same site. The answer to a problem I had but did not understand what would be the part (Char)0, 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;
            }
        }
    }
}

Follow the image:

Código

1 answer

2


And this you understand?

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

It’s 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 by default UTF-16 encoding, but the bulk of the characters we use are from ascii table, then each number refers to a character, and not all are printable, some are special and have some specific action done or determine something that is not printable, which is the case for everyone below code 32.

The 0 is null, the 8 is a tabulation and the 13 It’s a line change. The backslash is an escape character, in the syntax of much of the languages, including C#, it indicates that what comes next is something special and should not be treated as a character but as a coding table code or even some special term, for example 8 can be expressed as \t and 13 as \r, or in some cases such as \n, but there 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 not assign numbers or use as operand of char directly, you have to explicitly guarantee that the type being used is a char, so is made a cast in the number only to state to the compiler that you know what you are doing and that number must be interpreted as character.

Browser other questions tagged

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