等しいと==は参照平等をチェックします。 しかし、それはなぜ異なった動作をしますか?ここで 等しいvs ==は異なる動作をします
Console.WriteLine(c == d); //False
Console.WriteLine(c.Equals(d)); //True
Console.WriteLine(cc == dd); //True
Console.WriteLine(cc.Equals(dd));//True
誰かがシーンの背後に何が起こるかを説明することができます。など
//https://blogs.msdn.microsoft.com/csharpfaq/2004/03/29/when-should-i-use-and-when-should-i-use-equals/
public void StringDoubleEqualsVsEquals()
{
// Create two equal but distinct strings
string a = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
string b = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
Console.WriteLine(a == b); //True
Console.WriteLine(a.Equals(b)); //True
// Now let's see what happens with the same tests but with variables of type object
object c = a;
object d = b;
Console.WriteLine(c == d); //False
Console.WriteLine(c.Equals(d)); //True
/*************************************************************************/
Console.WriteLine(Environment.NewLine);
string aa = "1";
string bb = "1";
Console.WriteLine(aa == bb);//True
Console.WriteLine(aa.Equals(bb));//True
object cc = aa;
object dd = bb;
Console.WriteLine(cc.GetType());//System.String
Console.WriteLine(dd.GetType());//System.String
Console.WriteLine(cc == dd);//True
Console.WriteLine(cc.Equals(dd));//True
Console.ReadKey();
}
文字列 '=='演算子が値を参照していないかどうかをチェックします。https://msdn.microsoft.com/en-us/library/362314fe.aspx –
'Equals'と' == 'はどちらも参照用にテストされていません平等。 – Lee
文字列の場合は '' == 'は値の比較を行い、参照の比較は行いません。](https://msdn.microsoft.com/en-us/library/system.string.op_equality(v = vs.110).aspx ) –