Compare String and Object in C#

后端 未结 6 579
感情败类
感情败类 2020-11-30 22:30

See this code:

object x = \"mehdi emrani\";
string y = \"mehdi emrani\";
Console.WriteLine(y == x);

that returns true.

6条回答
  •  情深已故
    2020-11-30 23:06

    In the background a new string is created every time you modify an existing one, because strings are immutable, which means they can't change.

    See the following for an explanation: Why .NET String is immutable?

提交回复
热议问题