.NET String object and invalid Unicode code points

.net, string, unicode

Solution

Yes, it is possible. According to Microsoft's documentation, a .NET String is simply

A String object is a sequential collection of System.Char objects that represent a string.

while a .NET Char

Represents a character as a UTF-16 code unit.

Taken together, this means that a .NET String is just a sequence of UTF-16 code units, whether or not they are valid strings according to the Unicode standard. There are many ways this can occur, some of the more common ones I can think of are:

- A non UTF-16 byte stream being mistakenly put into a String object without proper conversion.

- A String object was split between a surrogate pair.

- Someone purposely included such a String to test the system's robustness.

As a result, the following C# code is completely legal and will compile:

class Test
    static void Main(){
        string s = 
            "\uEEEE" + // A private use character
            "\uDDDD" + // An unpaired surrogate character
            "\uFFFF" + // A Unicode noncharacter
            "\u0888";  // A currently unassigned character       
        System.Console.WriteLine(s); // Output is highly console dependent
    }
}

Problem

Is it possible that a .NET String object will contain an invalid Unicode code point? If yes, how this could happen (and how can I determine if the string has such invalid chars)?

Original source