C # performance: type comparison and string comparison

Which is faster? It:

bool isEqual = (MyObject1 is MyObject2)

Or that:

bool isEqual = ("blah" == "blah1")

It would be helpful to find out which one is faster. Obviously, if you apply .ToUpper () to each side of string comparisons, as programmers often do, this will require reallocation of memory, which affects performance. But what about if .ToUpper () goes out of the equation, as in the above example?

+3
source share
7 answers

I'm a little confused here.

As the other answers note, you are comparing apples and oranges. ::rimshot::

If you want to determine if an object has a specific type, use the operator is.

, == ( , - , ).

( ), , .


, ( , System.Object).

, , , . .NET C - , .

, , s "I'm a string", :

if (((object) s) == ((object) "I'm a string")) { ... }

, , s. , , , . , , . .

+9

.NET

bool isEqual = String.Equals("test", "test");

bool isEqual = ("test" == "test");

bool isEqual = "test".Equals("test");

, String.Equals, , , .

- ; , . . , , , .

+5

. , :


        string toto = "toto";
        string tata = "tata";
        bool isEqual = string.Compare(toto, tata, StringComparison.InvariantCultureIgnoreCase) == 0;    
        Console.WriteLine(isEqual);     
+4

?:)

.

+1

"==" . "" , , . Equals - , True, . , , , , .

0

, , , , .

, , , .

, , , . . - , , .

0

, " ": object.ReferenceEquals a. b.

Note. You need to understand what the difference is and that you probably cannot use reference comparison in most scenarios. If you are sure that this is what you want, it can be a little faster. You should try it yourself and see if it is really worth the trouble.

0
source

Source: https://habr.com/ru/post/1699592/


All Articles