public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Salary { get; set; }
}
// test1
private static void ObjectEqualityTest()
{
var e1 = new Employee();
e1.Name = "hamid";
var e2 = new Employee();
e2.Name = "hamid";
// o1 = o2; // if i do it then all wil be true
bool b1 = e1 == e2; // f
bool b2 = e1.Equals(e2); // f
bool b3 = object.ReferenceEquals(e1, e2); // f
//-------------------------------------------------//
int a = 10;
int b = 20;
bool a1 = a == b; // f
bool a2 = a.Equals(b); // f
bool a3 = ReferenceEquals(a, b); // f
b = 10;
a1 = a == b; // t
a2 = a.Equals(b); // t
a3 = ReferenceEquals(a, b); // f
//----------------------------------------------//
string firstName = "Mahesh";
string lastName = "Chand";
string authorName = firstName + " " + lastName;
string cloneAuthor = authorName.Clone().ToString();
Console.WriteLine(authorName); Console.WriteLine(cloneAuthor);
firstName = null;
var afterValue= String.Concat(firstName, firstName); // ""
//------------------------------------------------------------------//
int number = -123;
bool bb = false;
Object numberObject = number;
object boolObject = bb;
string author = "Mahesh Chand";
Object[] objs = new Object[] { 555, 444, 999 };
Console.WriteLine("Concatenate 1: {0}", String.Concat(numberObject));
Console.WriteLine("Concatenate multiple: {0}", String.Concat(numberObject, boolObject, author));
Console.WriteLine("Concatenate an array: {0}", String.Concat(objs));
//----------------------------------------------------------------//
string s1 = "a";
string s2 = "a";
bool c1 = s1 == s2; // t
bool c2 = s1.Equals(s2); // t
bool c3 = ReferenceEquals(s1, s2); // t
s2 = "b";
c1 = s1 == s2; // f
c2 = s1.Equals(s2); // f
c3 = ReferenceEquals(s1, s2); // f
//------------..............--------------------------------------------------//
object o = null;
object p = null;
object q = new Object();
var chech= ReferenceEquals(o, p); // t
p = q;
chech=ReferenceEquals(p, q); // t
chech= ReferenceEquals(o, p); // f
//----..........---------------------------------------------------------------------//
object o1 = null;
object o2 = new object();
//Technically, these should read object.ReferenceEquals for clarity, but this is redundant.
var r1= ReferenceEquals(o1, o1); //true
r1 = ReferenceEquals(o1, o2); //false
r1 = ReferenceEquals(o2, o1); //false
r1 = ReferenceEquals(o2, o2); //true
r1= o2.Equals(o1); //false
r1 = o2.Equals(o2); //true
r1 = o1.Equals(o1); //NullReferenceException
r1 = o1.Equals(o2); //NullReferenceException
}
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Salary { get; set; }
}
// test1
private static void ObjectEqualityTest()
{
var e1 = new Employee();
e1.Name = "hamid";
var e2 = new Employee();
e2.Name = "hamid";
// o1 = o2; // if i do it then all wil be true
bool b1 = e1 == e2; // f
bool b2 = e1.Equals(e2); // f
bool b3 = object.ReferenceEquals(e1, e2); // f
//-------------------------------------------------//
int a = 10;
int b = 20;
bool a1 = a == b; // f
bool a2 = a.Equals(b); // f
bool a3 = ReferenceEquals(a, b); // f
b = 10;
a1 = a == b; // t
a2 = a.Equals(b); // t
a3 = ReferenceEquals(a, b); // f
//----------------------------------------------//
string firstName = "Mahesh";
string lastName = "Chand";
string authorName = firstName + " " + lastName;
string cloneAuthor = authorName.Clone().ToString();
Console.WriteLine(authorName); Console.WriteLine(cloneAuthor);
firstName = null;
var afterValue= String.Concat(firstName, firstName); // ""
//------------------------------------------------------------------//
int number = -123;
bool bb = false;
Object numberObject = number;
object boolObject = bb;
string author = "Mahesh Chand";
Object[] objs = new Object[] { 555, 444, 999 };
Console.WriteLine("Concatenate 1: {0}", String.Concat(numberObject));
Console.WriteLine("Concatenate multiple: {0}", String.Concat(numberObject, boolObject, author));
Console.WriteLine("Concatenate an array: {0}", String.Concat(objs));
//----------------------------------------------------------------//
string s1 = "a";
string s2 = "a";
bool c1 = s1 == s2; // t
bool c2 = s1.Equals(s2); // t
bool c3 = ReferenceEquals(s1, s2); // t
s2 = "b";
c1 = s1 == s2; // f
c2 = s1.Equals(s2); // f
c3 = ReferenceEquals(s1, s2); // f
//------------..............--------------------------------------------------//
object o = null;
object p = null;
object q = new Object();
var chech= ReferenceEquals(o, p); // t
p = q;
chech=ReferenceEquals(p, q); // t
chech= ReferenceEquals(o, p); // f
//----..........---------------------------------------------------------------------//
object o1 = null;
object o2 = new object();
//Technically, these should read object.ReferenceEquals for clarity, but this is redundant.
var r1= ReferenceEquals(o1, o1); //true
r1 = ReferenceEquals(o1, o2); //false
r1 = ReferenceEquals(o2, o1); //false
r1 = ReferenceEquals(o2, o2); //true
r1= o2.Equals(o1); //false
r1 = o2.Equals(o2); //true
r1 = o1.Equals(o1); //NullReferenceException
r1 = o1.Equals(o2); //NullReferenceException
}
No comments:
Post a Comment