In C#, a string is a sequential read-only collection of Char objects. A Char object is an instance of a Char struct, which represents a character as a UTF-16 code unit.
Most programs, and programmers, do not care much about the internal representation of a string and a Char. As long as you know that strings are immutable and there is a big efficiency penalty involved in building up strings in loops without involving a mutable object, such as a StringBuilder, you are mostly fine.
However, there are some things that are good to know when working with strings. One of these things is comparison using methods such as String.CompareTo and String.Compare. By default the Compare and CompareTo methods are Culture sensitive. This means, that depending on the language and culture setting of the machine running the application, comparison and sorting of strings might be different than on your development machine. This might be exactly what you want, or it might come as a complete surprise.
The best thing to do here is to override the default behavior and be specific in your code on how you want your application to behave.
So, do not use the CompareTo method at all, instead use Compare, but not the default that only takes two strings, but the version where you explicitly define which type of comparison you want to use. This is, Compare(String, String, StringComparison).
For more info:
https://docs.microsoft.com/en-us/dotnet/api/system.string
Most programs, and programmers, do not care much about the internal representation of a string and a Char. As long as you know that strings are immutable and there is a big efficiency penalty involved in building up strings in loops without involving a mutable object, such as a StringBuilder, you are mostly fine.
However, there are some things that are good to know when working with strings. One of these things is comparison using methods such as String.CompareTo and String.Compare. By default the Compare and CompareTo methods are Culture sensitive. This means, that depending on the language and culture setting of the machine running the application, comparison and sorting of strings might be different than on your development machine. This might be exactly what you want, or it might come as a complete surprise.
The best thing to do here is to override the default behavior and be specific in your code on how you want your application to behave.
So, do not use the CompareTo method at all, instead use Compare, but not the default that only takes two strings, but the version where you explicitly define which type of comparison you want to use. This is, Compare(String, String, StringComparison).
For more info:
https://docs.microsoft.com/en-us/dotnet/api/system.string
Kommentarer
Skicka en kommentar