0

Possible Duplicate:
Differences in string compare methods in C#

I know that in C# strings are objects, and I just learned about the CompareTo method when comparing two strings. But what if I need to compare the string to a specific text value?

Now I'm doing this:

private string choice;

[...some code...]

if (choice == "1")
        {
            Console.WriteLine("You choose 1");
            Console.ReadLine();
        }
        else if (choice == "2")
        {
            Console.WriteLine("You choose 2");
            Console.ReadLine();
        }
etc...

And it works just as I want. But is it good coding? Can not find any information on this specific topic.

Community
  • 1
  • 1
Christoffer
  • 7,470
  • 9
  • 39
  • 55

2 Answers2

4

It is often better to compare string using the built-in compare mechanisms, as they give a clear statement on what you are doing.

For example:

public void Compare()
{
    string s1 = "2";
    string s2 = "" + 2;
    string.Equals(s1, s2, StringComparison.InvariantCultureIgnoreCase);
}

Everybody can see here, that you will ignore the case and culture is not relevant for this compare.

In your special case I would consider to use TryParse, as you will have a integer here:

int result;

if (Int32.TryParse(s1, out result))
{
    // choice is valid
}
Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113
  • I am fairly sure that a TryParse would be significantly slower than the string compares that OP is using. Probably wouldn't make much difference here as it looks like the compare only gets hit once, but in general. – Ryan O'Neill Jul 01 '12 at 20:26
  • It might be slower, but the code won't be executed that much and OP is clearly searching for an Integer value here, not a string, therefore the addon hint. – Mare Infinitus Jul 01 '12 at 20:47
1

You can safely compare a string reference to a string literal.

There are some edge cases where the compile time matters

string a = "this is a";
string b = a;
string c = "this is " + x; //x has the value of 'a'
string d = "this is a";
object oa = a;
object ob = b;
object oc = c;
object od = d;

console.WriteLine(a == b); //will print true
console.WriteLine(a == c); //will print true
console.WriteLine(c == b); //will print true

console.WriteLine(oa == ob); //will print true
console.WriteLine(oa == oc); //will print false
console.WriteLine(oa == od); //will print true

In the forst three lines is a value comparison and as long as the value of the string object is the same the result will be true.

In the last three lines it's a reference comparison, because the compile time type of the variables are object.

a and b are going to be the same object so the first comparison will return true. however oa and oc are not going to be the same object. There's no way for the compiler to determine that they are actually the same string value. The last line will also return true. This is because the compiler will realize at compile time that a and d has the same string value and will therefor only create one string object. It can safely do this because strings are immutable in C#

Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • in your example, x has no value assigned, especially not a – Mare Infinitus Jul 01 '12 at 19:55
  • @MareInfinitus hence the comment stating that the value is "a" how it got to be that is irrelevant to the example – Rune FS Jul 02 '12 at 05:29
  • Why don't you just provide a complete example? It would even be less to write. And I still think, that "==" is not a very good way of comparing in C#, even if it might work in a special case. – Mare Infinitus Jul 02 '12 at 06:30