If in C# "Enum"s only accept whole types, why accept a "char"?

Asked

Viewed 178 times

2

When we’re learning about enums in C# is said to accept only integer numeric types. Therefore, we soon think of sbyte, byte, short, ushort, int, uint, long or ulong. But the code below also works, where we associate elements of enum to a guy char. Why does this happen?

public enum PayCode {
    NotPaid = 'N',
    Paid = 'P'
}
  • In c# char is a 16-bit integer (2 bytes), each character is encoded in a numeric code (look for UNICODE encoding), so they are compatible types for conversion. There are numerous encodings, but the default for strings in c# is UNICODE. Curiosity: Emojis occupy 2 characters (look for surrogate).

1 answer

2


In fact the enum is using the type int and I can prove:

using static System.Console;
 
public class Program {
    public static void Main() {
        WriteLine(PayCode.NotPaid.GetTypeCode());
        WriteLine((int)(PayCode.NotPaid));
        WriteLine((char)(PayCode.NotPaid));
        Teste('A');
        int x = 'B';
    }
    public static void Teste(int x) {}
}
 
public enum PayCode {
    NotPaid = 'N',
    Paid = 'P'
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

So what happened? A promotion like char for a int. All char can be automatically considered as a numerical type larger than it, as the int if the site expects it. So, in addition to showing what is the internal type of the enum, used showing in other contexts where one expects int that send a char it’s okay.

This rule was defined by language. Is it correct? In other languages it was always easy to answer this because the type char actually it was just a number that by chance could be interpreted as if it were an ASCII table character. In C# it could be different since it has better abstractions, and has kind that was numerical and did not accept to convert automatically (bool), but they thought it best to keep the same semantics in this case.

And note that you can show the real value of the enumeration, if you really need to do this, saying how you want to show and works for both integer and character. But make no mistake, in memory you will have a saved integer where you have a value of that enumeration, as shown in the first line of the code.

  • I see the guy char as just a number, in which the implementation of ToString converts the same to a string containing the character corresponding to its code. It is wrong to think of the type char thus?

  • @user140828 is not wrong, in all languages [and seen this directly, although this idea of the ToString() That’s right, but you don’t need it to have a character, it’s just a form. In fact all types are only numbers that can be represented in some way, computer has nothing but numbers (which is already an abstraction). To me it’s just weird for the inconsistency I said. bool is just a number that has two values that you interpret as true or false, but it’s only 0 or 1, and the language doesn’t let you do int x = true.

  • Just to complement, as I understand it, "under the table" Char is a type ushort, it accepts exactly the same range of values (0-65.535).

Browser other questions tagged

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