Recall & Review
beginner
What does the
== operator do when used with strings in C#?In C#, the
== operator compares the contents of two strings to check if they are equal, not just their references. It returns true if the strings have the same characters in the same order.Click to reveal answer
intermediate
What is the difference between
String.Equals() and == for strings?String.Equals() is a method that compares two strings for equality and can be customized with options like case sensitivity. The == operator also compares string contents but is simpler to use. Both check the actual text, not references.Click to reveal answer
intermediate
How can you compare two strings ignoring case in C#?
Use
String.Equals(str1, str2, StringComparison.OrdinalIgnoreCase) to compare two strings without considering uppercase or lowercase differences.Click to reveal answer
intermediate
What does
String.Compare(str1, str2) return?It returns an integer:
0 if strings are equal, less than 0 if str1 is less than str2, and greater than 0 if str1 is greater than str2 based on lexicographical order.Click to reveal answer
advanced
Why should you avoid using
ReferenceEquals() to compare strings for equality?ReferenceEquals() checks if two string variables point to the exact same object in memory, not if their text is the same. This can give false results when strings have the same content but are different objects.Click to reveal answer
Which operator compares the content of two strings in C#?
✗ Incorrect
The == operator compares the content of two strings for equality in C#.
What does
String.Equals(str1, str2, StringComparison.OrdinalIgnoreCase) do?✗ Incorrect
It compares two strings ignoring case differences.
What value does
String.Compare(str1, str2) return if str1 is lexicographically less than str2?✗ Incorrect
It returns a negative number if str1 is less than str2.
Which method should you use to check if two strings have the same text but ignore case?
✗ Incorrect
Using String.Equals with StringComparison.OrdinalIgnoreCase ignores case when comparing.
Why is
ReferenceEquals() not reliable for string content comparison?✗ Incorrect
ReferenceEquals checks if two variables point to the same object, not if their text is equal.
Explain how to compare two strings for equality in C# considering case sensitivity.
Think about methods and operators that check string content.
You got /3 concepts.
Describe the difference between comparing strings by reference and by content in C#.
Consider what happens when two strings have same text but different memory locations.
You got /3 concepts.