pythonc#charord

Is there a C# equivalent of Pythons chr and ord?


I'm currently learning C# and was wondering if C# has an equivalent to chr and ord.


Solution

  • In C#, char is efficiently UInt16; that's why we can simply cast:

    chr: (char) explicit cast (if i is out of [0..UInt16.MaxValue] range we'll have integer overflow)

     int i = ...
     char c = (char) i; 
    

    ord: either (int) or even implicit cast (cast from char to int is always possible)

     char c = ...
     int i = c;