0
0
C Sharp (C#)programming~5 mins

String comparison and equality in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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#?
A==
B=
C===
D!=
What does String.Equals(str1, str2, StringComparison.OrdinalIgnoreCase) do?
ACompares strings including case
BChecks if strings are the same object
CCompares strings ignoring case
DConverts strings to uppercase
What value does String.Compare(str1, str2) return if str1 is lexicographically less than str2?
ALess than 0
B0
CGreater than 0
DThrows an exception
Which method should you use to check if two strings have the same text but ignore case?
AReferenceEquals()
BString.Equals() with StringComparison.OrdinalIgnoreCase
C== operator
DString.Compare() without parameters
Why is ReferenceEquals() not reliable for string content comparison?
AIt compares string lengths only
BIt always returns true
CIt ignores case differences
DIt compares memory addresses, not content
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.