C#中的==和Equals()
class Program
{
public static bool EqualityTest1( object o1, object o2)
{
return o1.Equals(o2);
}
public static bool EqualityTest2(object o1 , object o2)
{
return o1 == o2;
}
static void Main(string[] args)
{
int i1 = 4;
int i2 = 4;
string s1 = "Hello!" ;
string s2 = "Hello!";
string s3 = new string (new char[] { 'H', 'e', 'l' , 'l', 'o', '!' });
//Now let's look at some simple methods to compare equality.
Console.WriteLine(EqualityTest1 (i1, i1));
Console. WriteLine(EqualityTest2(i1, i1));
// Compare i1 to i2
Console.WriteLine(EqualityTest1(i1, i2 ));
Console.WriteLine(EqualityTest2(i1, i2));
// Compare s1 to s2
Console.WriteLine(EqualityTest1 (s1, s2));
Console. WriteLine(EqualityTest2(s1, s2));
// Compare s2 to s3
Console.WriteLine(EqualityTest1(s2, s3 ));
Console.WriteLine(EqualityTest2(s2, s3));
}
}
{
public static bool EqualityTest1( object o1, object o2)
{
return o1.Equals(o2);
}
public static bool EqualityTest2(object o1 , object o2)
{
return o1 == o2;
}
static void Main(string[] args)
{
int i1 = 4;
int i2 = 4;
string s1 = "Hello!" ;
string s2 = "Hello!";
string s3 = new string (new char[] { 'H', 'e', 'l' , 'l', 'o', '!' });
//Now let's look at some simple methods to compare equality.
Console.WriteLine(EqualityTest1 (i1, i1));
Console. WriteLine(EqualityTest2(i1, i1));
// Compare i1 to i2
Console.WriteLine(EqualityTest1(i1, i2 ));
Console.WriteLine(EqualityTest2(i1, i2));
// Compare s1 to s2
Console.WriteLine(EqualityTest1 (s1, s2));
Console. WriteLine(EqualityTest2(s1, s2));
// Compare s2 to s3
Console.WriteLine(EqualityTest1(s2, s3 ));
Console.WriteLine(EqualityTest2(s2, s3));
}
}
运行结果有点出人意料,但是还是解释的通的。
没有评论:
发表评论