[c#] Comparing strings in C# with OR in an if statement

I'm a newbie to C#, but can't seem to find anything about this problem. Here's what I'm trying to do:

string testString = txtBox1.Text;
string testString2 = txtBox2.Text;

if ((testString == "") || (testString2 == ""))
{
    MessageBox.Show("You must enter a value into both boxes");
    return;
} 

Basically I need to check to see if either txtBox1 or txtBox2 is blank. However I'm getting an error when running this. What's the proper way to do this (or is my approach all wrong)?

This question is related to c# string

The answer is


Since you want to check whether textboxes contains any value or not your code should do the job. You should be more specific about the error you are having. You can also do:

if(textBox1.Text == string.Empty || textBox2.Text == string.Empty)
   {
    MessageBox.Show("You must enter a value into both boxes");
   }

EDIT 2: based on @JonSkeet comments:

Usage of string.Compare is not required as per OP's original unedited post. String.Equals should do the job if one wants to compare strings, and StringComparison may be used to ignore case for the comparison. string.Compare should be used for order comparison. Originally the question contain this comparison,

string testString = "This is a test";
string testString2 = "This is not a test";

if (testString == testString2)
{
    //do some stuff;
}

the if statement can be replaced with

if(testString.Equals(testString2))

or following to ignore case.

if(testString.Equals(testString2,StringComparison.InvariantCultureIgnoreCase)) 

Similar questions with c# tag:

Similar questions with string tag: