6. .NET Fundamentals || C#10 Flashcards
What is the difference between char.ToUpper() and char.ToUpperInvariant() ?
When dealing with different culture charaters sometimes it can reference a different character. e.g. to upper of ‘i’ in Turkey is “I’ with a dot on top, which is totally different char than simple “I” in English. To avoid that we can call methods that end in “Invariant” such as ToUpperInvariant which will always return the English cultures result (in this case a simple “I”)
Is string a mutable or immutable type?
String is a immutable (unchangable) sequance of characters
Can string be null?
Since string a reference type, it can be null:
string s = null; // OK
Can we use ‘foreach’ to iterate through strings character? Why?
Yes, we can. This is because string implements IEnumerable<char> :
foreach (char c in "123") Console.WriteLine(c + ","); // 1,2,3,</char>
What happens when you edit a string under the hood?
Since string is an immutable type, each time you “manipulate” it a string returns a new one, leaving the original untouched (the same goes when you reassign a string variable)
What does string.Substring(a, b)
and string.Substring(a)
do?
Extracts a portion of string:
~~~
string s = “abcdef”;
s.Substring(0, 3) = “abc”
~~~
OR gets the remainder if we put only one parameter:s.Substring(2) = "cdef";
What does string.Insert() and string.Remove() do?
Inserts and Removes characters at specified position:
string s = “abcdef”;
s.Insert(3, “, “) = “abc, def” // inserts before the 3rd char
Remove(where, howMuch):
s.Remove(0, 2) = “cdef”
What does string.PadLeft() and string.PadRight() do?
Pad a string to a given length with a specified character (if the second paramenter is not given - the space is inserted):
string s = “abcdef”; // Length == 6
s.PadLeft(8, ‘-‘) = “–abcdef” // Length == 8
If the given string is longer than what is specified in PadLeft/PadRight the string is returned unchanged
What does (string) TrimStart, TrimEnd and Trim do?
TrimStart and TrimEnd remove specified characters from the beginning or end of a string; Trim does both. By default these functions remove whitespace characters (including spaces, tabs, new lines and Unicode variations of these):
“ abc \t\r\n “.Trim() // “abc”
What does (string) Replace() do?
Replaces all (nonoverlapping) occurrences of a particular character or substring:
“to be done”.Replace(“ “, “-“) // “to-be-done”
What does (string) ToUpper or ToLower do?
Returns uppercase or lowercase variant of the string while respecting the current language settings. To always apply English language settings ToUpperInvarriant or ToLowerInvariant should be used
How does string.Split() works?
Split divides string into pieces. By default it uses white space characters as delimiters;
Split also accepts StringSplitOptions enum which has an option to remove empty entries;
this is useful when words are separated by several delimiters in a row
string[] words = “The quick brown fox”.Split();
foreach (string w in words)
Console.WriteLine(w + “|”); // The|quick|brown|fox
How string.Join() and string.Concat() work?
The Join method works opposite of Split() - it requires a delimiter and a string array;
string[] words = “The”, “quick”, “brown”, “fox”;
string w = string.Join(“ “, words);
Concat method is exactly equivalent to the + operator and accepts only a params string array and applies no separator (to be exact the compiler translates + to Concat):
string.Concat(“The”, “quick”, “brown”, “fox”) ==
“The” + “quick” + “brown” + “fox”;
What is a composite format string?
The master string that includes the embedded variables. It is called using string.Format method
string masterString= “It’s {0} degrees in {1} on this {2} morning”;
string s = string.Format(masterString, 5, Utena, DateTime.Now.DayOfWeek);
// “It’s 5 degrees in Utena on this Saturday morning”
Each number in curly brackets is called a format item. The number corresponds to the arguments position. There could be second parameter which can be:
1. A comma and a minimum width to apply
2. A colon and a format string
The minimul width is useful when aligning columns - if the value is negative the data is left-aligned , otherwise - right-aligned
string s = “Name={0, -20}, Credit Limit={1, 15:C}”
CW(string.Format(s, “Inga”, 2000)); // “Name=Inga Credit Limit = $2, 000.00”
What are the two basic algorithms for string comparisons? Define them
Ordinal and culture sensitive algorithms
- Ordinal comparisons interpret characters simply as numbers (according to their Unicode value); == operator and Equals() always performs ordinal case-sensitive comparison; comparison scheme : (aAbBcC…)
- Culture sensitive comparisons interpret characters with reference to a particular alphabet. There are two special cultures: the “current culture” which is based on a settings picked up from computers control panel and the “invariant culture” which is the same on every computer (closely matches American culture) comparison scheme : (ABCDabcd)
Are the string.Equal() and string.CompareTo() methods the same?
No.
Equals() metod performs ordinal oder comparison to string. Returns true/false.
CompareTo() method performs culture-sensitive, case-sensitive order comparison. Implements IComparable interface, a standart comparison protocol used across .NET libraries.
Therefore if we apply this method on “ū” and “u” the strings wouldn’t be equal. Returns -1, 0 or 1 depending if the value comes after, before or alongside the second value
What is StringBuilder?
The StringBuilder class (System.Text namespace) represents a mutable (editable) string. With a StringBuilder you can Append, Insert, Remove and Replace substrings without replacing the whole StringBuilder
Which is better to call string.Concat() or StringBuilder.Append()
StringBuilder.Append() is much more efficient than repeatedly concatenating ordinary string types.
Each time we modify a regular string, a new string is created since string is immutable. But when we are using stringBuilder (mutable type) we can add new values at the end or at specific point without creating new StringBuilder object.
Let’s say we have a StringBuilder which has around 2million characters inside (that accumulates around 2MB of memory). We set it’s Length to 0, what’s its allocated memory right now?
The same 2MB. Since setting Length to 0 does not release the memory. If we want to do that we need to create a new string builder and allow the old one to drop out of scope (and be garbage collected)
What is a text encoding?
A text encoding maps characters from their numeric code point to a binary representation. In .NET text encodings come into play primarily when dealing with text files or streams. When you read a text file into a string a text encoder translates the file data from binary into the internal Unicode representation that the char and string types expect.
There are two types of text encodings in .NET:
1. Those that map Unicode characters to another character set
2. Those that use standart Unicode encoding schemas (UTF-8, UTF-16 and UTF-32 (and obsolete UTF-7)) Each differs in space efficiency. UTF-8 is the most efficient - it uses between one and four bytes to represent each character; First 128 character require only one byte, which makes it compatible with ASCII
What is a high-surrogate and a low-surrogate?
A high surrogate and a low surrogate are two 16-bit characters that are used together to represent a single character in the Unicode range U+010000 to U+10FFFF.
The high surrogate represents the first half of the character code, and the low surrogate represents the second half. These characters are also known as lead and trail surrogates, respectively.
In C#, high surrogates have a value in the range U+D800 to U+DBFF, and low surrogates have a value in the range U+DC00 to U+DFFF. When combined, the high and low surrogates form a single character that can be used to represent characters outside the BMP range.
For example, the Unicode character ‘🙂’ (SMILING FACE WITH SMILING EYES) can be represented as a surrogate pair in C#:
char highSurrogate = ‘\uD83D’;
char lowSurrogate = ‘\uDE42’;
string smiley = new string(new char[] { highSurrogate, lowSurrogate });
Which structs are used to represent dates and times?
DateTime, DateTimeOffset, TimeSpan, DateOnly and TimeOnly. All of them are immutable