The SLAR on System.Char


Continuing in the series on sharing some of the information in the .NET Framework Standard Library Annotated Reference Vol 1 here is an annotation from the System.Char class. 

 

BA – In the design of this type we debated having all the predicates (IsXxx methods)

in this class, versus putting them in another class. In the end we felt the simplicity of

having just one class for all these operations made it worthwhile.

 

Jeff RichterNote that Char is not the most accurate name for this type. A Char is really a

UTF-16 code point, which will be a character unless the code point represents a high

or low surrogate value. A string is really a set of UTF-16 code points. To properly

traverse the characters of a string, you should use the System.Globalization.

StringInfo class’s methods.

 

And here is a sample, pretty simple, but interesting none the less

 

 

using System;

 

namespace Samples

{

  public class CharGetNumericValue

  {

    public static void Main()

    {

      Char c = ‘3’;

      Console.WriteLine(

                “Numeric value of Char ‘{0}’ is {1}”,

                c, Char.GetNumericValue(c));

      c = Convert.ToChar(0X00BC);

      Console.WriteLine(

                “Numeric value of Char ‘{0}’ is {1}”,

                c, Char.GetNumericValue(c));

      c = Convert.ToChar(0X03a0);

      Console.WriteLine(

                “Numeric value of Char ‘{0}’ is {1}”,

                c, Char.GetNumericValue(c));

      c = ‘A’;

      Console.WriteLine(

                “Numeric value of Char ‘{0}’ is {1}”,

                c, Char.GetNumericValue(c));

    }

  }

}

And the output:

Numeric value of Char ‘3’ is 3

Numeric value of Char ‘¼’ is 0.25

Numeric value of Char ‘?’ is -1

Numeric value of Char ‘A’ is -1

 

Comments (2)

  1. Kent Boogaart says:

    Interesting stuff. I kind of expected char.GetNumericValue(0x221e) to return double.PositiveInfinity but it returns -1.

  2. Joe White says:

    Just curious: what was the reason behind only having static methods, as opposed to providing both static methods and instance properties? Sometimes an instance property would make the code a little easier to read, and some BCL classes (like Regex) do provide both.