F#'s System.Char Type Basics


If you are coming from a .NET background and using C# most of the time you have probably encountered characters or Char type within the .NET space.

If your answer is yes, I assume you have used the System.Char including its common methods.

However, if your answer is no, no worries. In this article, we’ll discuss characters or Char type within the .NET using version 6 and the language F#.

We’re going to cover the following: how to define characters, character escape sequences, and how to represent a .NET character's Unicode value in a numerical value.

OK, let’s get started then.

How to Define Characters

One good thing to remember when we say characters. Just imagine that every time you declare a type Char, it stores a single text.

Now, a way to define a Char is assigned using single quotes around the literal value (character literal).

Of course, another way is by calling a function that returns a type, Char.

Let’s see some examples.

    This example shows how to declare a character or System.Char type.
let ``Declare Character`` () = 
    let letter = 'A'
    Assert.IsType(typedefof<char>, letter)
    This example shows how to declare a character or System.Char type
    but by calling a function. 
let ``Declare Character By Calling A Function`` () = 
    let funcReturnChar () = 'E' 

    let vowel = funcReturnChar()
    Assert.IsType(typedefof<char>, vowel)

That’s how easy it is, isn’t it?

Printing Emojis

Now let’s try to explore how we can print emojis.

But before that, I just want to emphasize that you can specify a Char value with a Unicode sequence which is \u, and then followed by the four-symbol hexadecimal representation of that certain character code.

OK, now let’s see those emojis in action. 😊

    This example prints emojis. Yehey!
    Unicode Character 'White Smiling Face' (U+263A) = '☺'
    Unicode Character 'Right-Facing Armenian Eternity Sign' (U+058D) = '֍'
let ``Print Characters`` () =
    (* a Unicode escape sequence, which is \u followed by the four-symbol hexadecimal representation of a character code. *)
    let smilingFace = '\u263A' (* ☺ *)
    let emoji1 = sprintf "Unicode Character 'White Smiling Face' (U+263A) = '%c'" smilingFace

    (* a Unicode escape sequence, which is \u followed by the four-symbol hexadecimal representation of a character code. *)
    let eternitySign = '\u058d'(* ֍ *)
    let emoji2 = sprintf "Unicode Character 'Right-Facing Armenian Eternity Sign' (U+058D) = '%c'" eternitySign

Just in case you missed it. Here's the output

Unicode Character 'White Smiling Face' (U+263A) = '☺'

Unicode Character 'Right-Facing Armenian Eternity Sign' (U+058D) = '֍'

Special Control Characters

For the special control characters, see the table below.

Character Meaning
\’ Single quote
\” Double quote
\\ Backslash
\b Backspace
\n Newline
\r Carriage return
\t Horizontal tab

Let’s see the example below on how we can play with the special control characters.

let ``Character Escape Sequences`` () = 
    let singleQuote = '\''

    Assert.Equal(singleQuote, '\'')
    output.WriteLine(sprintf "Single Quote: %c" singleQuote)

    let doubleQuote = '\"'

    Assert.Equal(doubleQuote, '\"')
    output.WriteLine(sprintf "Double Quote: %c" doubleQuote)

    let backSlash = '\\'

    (* Both assert/checks are the same*)
    (* or *)

    Assert.Equal(backSlash, '\\')
    output.WriteLine(sprintf "Backslash: %c" backSlash)

    let backSpace = '\b'

    output.WriteLine(sprintf "Backspace: %c" backSpace)

    let newline = '\n'

    Assert.Equal(newline, '\n')
    output.WriteLine(sprintf "Newline: %c" newline)

    let carriageReturn = '\r'

    Assert.Equal(carriageReturn, '\r')
    output.WriteLine(sprintf "Carriage Return: %c" carriageReturn)

    let horizontalTab = '\t'

    Assert.Equal(horizontalTab, '\t')
    output.WriteLine(sprintf "Horizontal Tab: %c" horizontalTab)


What's the Difference between /r and /r/n?

The main difference between the two is the platform. 

We have to remember that \r\n is for non-Unix platforms, while \n is for Unix platforms. 

In other words, Windows platforms use \r\n while Unix-related platforms use \n when creating a new line. 

See our example code below to prove our point. 

let ``Character NewLine in Different Platform`` () =

    (* If you are inside a Unix similar operating system*)
    if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
        RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || 
        RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) then 
        let newline = '\n'

        Assert.Equal(newline, System.Environment.NewLine[0])
        output.WriteLine(sprintf "Linux Machine's ('\\n) %c is equals %c(Environment.NewLine[0])." newline Environment.NewLine[0])
    else (* Windows *) 
        let newline = "\r\n"
        Assert.Equal(newline, System.Environment.NewLine)
        output.WriteLine(sprintf "Windows Machine's ('\\r'\\n) %s is equals %s(Environment.NewLine)." newline System.Environment.NewLine)

For the code sample above, we have to determine first the operating system then from there, we have proved the \r\n works for Windows while \n works for Unix-related platforms. 


See the output for the Windows platform. 

See the output for the Ubuntu/Unix-related platform. 

Numeric Representation of .NET Character

Sometimes we wanted to get the numerical value of a certain character, especially when we wanted to check if the character is an ASCII. 

For us to easily understand this, let's see the example below. 

let ``Numeric Representation of Character`` () = 
    let letterZ = 'Z'

    let numericalValue = (int letterZ)

    Assert.IsType(typedefof<int>, numericalValue)
    Assert.Equal(numericalValue, 90)


In this article, we have discussed the following

  • How to Define Characters
  • Special Control Characters
  • Numeric Representation of .NET Character

I hope you have enjoyed this article, as I enjoyed it while writing. You can also find the sample code here on GitHub.

Till next time, happy programming!

Similar Articles