had an argument with someone on how to compare string values in c# and i want to get some opinions which option is better and why:
== or string.Equals(string) ?
had an argument with someone on how to compare string values in c# and i want to get some opinions which option is better and why:
== or string.Equals(string) ?
== is better because it's more concise. functionally they're the same
ok but i still don't get why the docs are suggesting "Equals" then
One compare references, other values.
that's what the whole argument was about, because in the case of strings Equals actually compares values
== is more concise but is only overloaded where it makes sensen(datetime, string)
You have to follow this:
>msdn.microsoft.com
Equals check for full equality and == check for reference equality is the general case.
The same way == can be abused can be applied to the implementation of equals
which docs? have you tried asking stackexchange?
docs.microsoft.com
this guide suggests using equals
>When you test for equality of strings, you should use the methods that explicitly specify what kind of comparison you intend to perform. It makes your code is much more maintainable and readable.
This is true for all production code that deal with diferent languages and encodings.
== is the simplest form when you want to compare in-program strings and hardcoded settings.
Equals allows for setting the comparison method and is good practise but it might throw an exception if used on a null object hence one must take care of that when using it.
Java doesn't have this problem.
java has more problems to worry about
figuring out what == does is not one of them
Wrong. Figuring out why == is NOT working on strings is a problem in java
>stackoverflow.com
>stackoverflow.com
So, that is also another problem.
>Wrong. Figuring out why == is NOT working on strings is a problem in java
You must be a novice or a retard to actually be confused by this. The semantics of == and .equals are consistent and simple in Java, unlike in C#.
You must autistic and a novice in C# to not know what == is doing.
You are wrong.
>msdn.microsoft.com
Apparently these are the rules.
>== compares values for structs
>== should compare references for classes
>except where the classes are "base types", whatever that means (doesn't seem to mean base class so idk)
>and also if the class is numeric
>by the way all of this is just convention it can actually do whatever it likes
If it weren't confusing, OP wouldn't have made a thread.
Which proves you are novice.
Google "C# == operator not working" and try the same for java.
The thing is that C# also supports Equals whereas in java, not having operator overloading causes shit like .add() .subtract()..etc
in .NET == and .Equals() are identical for strings. Both compare content neither compares references
this is bery confusing reeeeeee
= (addition), == (test for equality)
>why program hard
>why grog confused
>why "a" == "a" not false
>== is reference equality
>different strings are different objects even if they happen to contain the same characters
>never use == unless reference equality is the point, like with some sort of highly specialized Map
wow so hard
Yet more java devs fall for string comparison issue than C# devs.
Pic related
because java programmers don't see sharp
*badum tsss*
kek