Questions to check your knowledge of .NET strings

Andrey Akinshin

I am always amused by .NET developers who say that they know everything about the strings. There are some of them who really know everything. But usually I can easily ask several questions after hearing which my companion loses confidence in his knowledge. I offer a list of some simple questions that check knowledge of the System.String type.
 
Questions_to_check_your_knowledge_of_NET_strings

 
Question:

Does the comparison of upper case strings and lower case strings differ? Will it take different time to execute the following commands: “abc1″ == “abc2″ and “ABC1″ == “ABC2″?
Answer:

In FCL comparison of upper case strings is optimized. In common cases it is better to use the upper case. But the commands will be executed equally fast in this exact sample: optimization influences only specific locale identifiers.
 
Question:

Let us have an array of various strings. May the sorting method get different results from time to time?

Answer:
Yes, standard .NET sorting is unstable. It means that if we sort strings regardless of case, then “AAA” and “aaa” strings can be positioned in any order.
 
Question:

Can the String.CompareTo get 0 for the strings of different length?
Answer:

Yes, it can. For example, strings “ß” and “ss” will match in German locale identifier.
 
Question:

Is it possible that two strings match by the String.CompareTo method, but differ by String.Equals method?

Answer:

Yes, it’s possible since CompareTo is executed regarding the regional standards by default while Equals doesn’t consider these standards.
 
Question:

Is comparison of strings after adjusting to upper or lower case equal to comparison of strings regardless of case?

Answer:

No, this trick won’t work in some locale identifiers. See The Turkey Test.
 
Question:

Let us have two strings of Latin lower case characters. How to compare them faster?

Answer:

Use the StringComparison.Ordinal method, since in this case there is no need to consider case and regional settings.
 
Question:

Will CurrentUICulture or CurrentCulture be used by default when formatting date (DateTime.ToString())?
Answer:
CurrentCulture will be used since this sample is not related to GUI.
 
Question:
What’s the difference between
string s = “Line1\nLine2″;
and
string s = “Line1″ + Environment.NewLine + “Line2″;

Answer:

The second variant is more common as it doesn’t depend on platform.
 
Question:

How to add a back slash to the string without using escape sequence?

Answer:
It’s necessary to use verbatim string: @”\”.
 
Question:

Can the following code:
Console.WriteLine(“Hello”);
display a string different to “Hello” using the standard Console.WriteLine method?

Answer:

Yes, it can. It’s possible if the string is interned and someone got to the hash table of interned strings via the uncontrolled memory and changed the target value.
 
Question:

If we mark the assembly with the attribute System.Runtime.CompilerServices.CompilationRelaxationsAttribute with the flag CompilationRelaxations.NoStringInterning from the same namespace, does it mean that literal strings in this assembly won’t intern?

Answer:

No, it doesn’t. ECMA specification states that in this case CLR is able not to intern all strings, but it’s not obliged. Besides, it’s always possible to intern the string using the String.Intern method.
 
Question:

If we disable intern mechanism for the definite version of CRL and the literal string “Hello” appears in the code for two times, how many times will it appear in the assembly metadata.

Answer:

Only once. Interning of the string happens when the program runs and is not related to metadata.
 
Question:
Will the symbol array be copied when the StringBuilder.ToString() method is called?

Answer:

In .NET 2.0: No, it won’t. A new string will refer to the same symbol array as an initial StringBuilder.
In .NET 4.0: Yes, the array is always copied in this version.
 
Question:

Let us have StringBuilder, which stores a three-symbol string. Is there a scenario where substitution of the first symbol causes memory allocation for a new symbol array?

Answer:

Yes, there is. If the previous method is StringBuilder.ToString() and you use .NET lower than 4.0.
 
Question:

You can’t store confidential data (for example, password) in ordinary strings since a string is stored in memory insecurely and hackers can easily get it. What standard options can be used to secure the data?

Answer:

It’s necessary to store data using the SecureString class.
 
Question:

Is it possible that the amount of text elements (that can be got via TextElementEnumerator) of the string differ from the quantity of char-symbols forming it?

Answer:

Yes, it’s possible. FCL supports encodings using more than 16 bits: a single text element can be determined by several char-symbols.
 

August 1st, 2013

Leave a Comment