fork download
  1. using static System.Console;
  2.  
  3. public class Program {
  4. public static void Main() {
  5. // cria duas variáveis iguais mas distintas uma da outra
  6. string a = new string(new char[] {'h', 'e', 'l', 'l', 'o'});
  7. string b = new string(new char[] {'h', 'e', 'l', 'l', 'o'});
  8. WriteLine (a == b);
  9. WriteLine (a.Equals(b));
  10. // o mesmo teste usando os mesmo dados mas como variáveis do tipo `Object`
  11. object c = a;
  12. object d = b;
  13. WriteLine (c == d);
  14. WriteLine (c.Equals(d));
  15. WriteLine("-----------------");
  16. int myInt = 1;
  17. short myShort = 1;
  18. object objInt1 = myInt;
  19. object objInt2 = myInt;
  20. object objShort = myShort;
  21. WriteLine(myInt == myShort); // cenário 1 true
  22. WriteLine(myShort == myInt); // cenário 2 true
  23. WriteLine(myInt.Equals(myShort)); // cenário 3 true
  24. WriteLine(myShort.Equals(myInt)); // cenário 4 false!
  25. WriteLine(objInt1 == objInt1); // cenário 5 true
  26. WriteLine(objInt1 == objShort); // cenário 6 false!!
  27. WriteLine(objInt1 == objInt2); // cenário 7 false!!!
  28. WriteLine(Equals(objInt1, objInt2)); // cenário 8 true
  29. WriteLine(Equals(objInt1, objShort)); // cenário 9 false!?!
  30. WriteLine("-----------------");
  31. string s1 = "abc";
  32. string s2 = "abc";
  33. WriteLine(object.ReferenceEquals(s1, s2)); //retorna true
  34. string s3 = "abc";
  35. string s4t = "ab";
  36. string s4 = s4t + "c";
  37. WriteLine(object.ReferenceEquals(s3, s4)); //retorna false
  38. WriteLine(s3 == s4); //retorna true
  39. }
  40. }
  41.  
  42. //https://pt.stackoverflow.com/q/18910/101
Success #stdin #stdout 0.02s 16152KB
stdin
Standard input is empty
stdout
True
True
False
True
-----------------
True
True
True
False
True
False
False
True
False
-----------------
True
False
True