You have changed the value to an instance .
myString.Contains(anotherString)
Here, myString is the instance you call the Contains method on, while anotherString is the value passed to the method. If this value is null , the method will raise an ArgumentNullException .
When changing the instance to null , on the other hand, this will certainly lead to NRE, since you cannot call any of the null reference. However, if you set it to string.empty Contains , it will return false because the empty string does not contain anything (in particular string.empty does not contain "Hello World" , however "Hello World" contains the empty string).
So false returned:
Console.WriteLine(string.Empty.Contains("Hello World"));
So far, this returns true :
Console.WriteLine("Hello World".Contains(string.Empty));
In any case, what do you want to check if the empty IS string is contained in any other:
var retVal = myString.Contains(string.empty);
Which should return true .
In addition, myString.Contains(null) ArgumentNullException
On the other hand, null.Contains(aString) results in an NRE.
HimBromBeere
source share