关于C#中==与Equals方法的区别总结
来源:互联网 发布:网络协议与分析 编辑:程序博客网 时间:2024/06/16 00:26
为了保护原创作者的原创性,请您先参考一下文章,之后回来做一个讨论交流:
http://www.cnblogs.com/jiahaipeng/archive/2008/04/11/1146316.html#commentform
为了方便理解,我把源代码粘贴过来:
//class Person
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
ConsoleApplication1
{
class
Person
{
private
string
name;
public
string
Name
{
get
{
return
name; }
set
{ name = value; }
}
public
Person(
string
name)
{
this
.name = name;
}
}
}
//Main
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(
string
[] args)
{
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);
Console.WriteLine(a.Equals(b));
object
g = a;
object
h = b;
Console.WriteLine(g == h);
Console.WriteLine(g.Equals(h));
Person p1 =
new
Person(
"jia"
);
Person p2 =
new
Person(
"jia"
);
Console.WriteLine(p1 == p2);
Console.WriteLine(p1.Equals(p2));
Person p3 =
new
Person(
"jia"
);
Person p4 = p3;
Console.WriteLine(p3 == p4);
Console.WriteLine(p3.Equals(p4));
Console.ReadLine();
}
}
}
输出结果:true,true,false,true,false,false,true,true。其详细解释请参考一下网页:
http://www.cnblogs.com/jiahaipeng/archive/2008/04/11/1146316.html#commentform
总结如下:
1、对于值类型,==和equals等价,都是比较存储信息的内容。
2、对于引用类型,==比较的是引用类型在栈中的地址,equals方法则比较的是引用类型在托管堆中的存储信息的内容。
3、对于string类要特殊处理,它是一个内部已经处理好了equals方法和==的类,故==和equals等价,都是比较存储信
息的内容。
4、对于一些自定义的类,我们有必要重载equals方法,否则它默认为基类的equals方法(基类没有重载Equals方法则为
Object类中的Equals方法),他们的比较也为地址,而不是引用类型在托管堆中的存储信息的内容。故我们就不难理解
以下输出了:
Person p1 = new Person("jia");
Person p2 = new Person("jia");
Console.WriteLine(p1 == p2);//输出False
Console.WriteLine(p1.Equals(p2));//输出False
5、对于string我们有必要强调一下常量字符串与字符串变量的区别,请看一下例子:
//常量字符串
string x = "should it matter"; //指向同一个地址,即所谓的常量池
string y = "should it matter";
object c = x;
object d = y;
Console.WriteLine(c == d);//输出True
Console.WriteLine(c.Equals(d));//输出True
//字符串变量
string a = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });//指向的地址不一样,是动态分配的
string b = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
object g = a;
object h = b;
Console.WriteLine(g == h);//输出False
Console.WriteLine(g.Equals(h));//输出True
他们分配地址的方式不一样,string x = "should it matter";由于它的初始值是一个常量,所以其地址分配在托
管堆上的静态存储区,即所谓的常量池,而楼主的string a = new string(new char[] { 'h', 'e', 'l', 'l', 'o'});则不
一样,它这是在托管堆上动态分配的地址。
详细的解释可以参考一下网址或者查阅msdn:
http://www.cnblogs.com/instance/archive/2011/05/24/2056091.html
- 关于C#中==与Equals方法的区别总结
- C#中==与Equals方法的区别
- C#中==与Equals区别
- 在C#中Equals与==的区别?
- c#中equals与==的区别
- C#中==与Equals的区别....
- c# equals与==的区别
- c# equals与==的区别
- c# equals与==的区别
- C# equals 与 == 的区别
- c# equals与==的区别
- c# equals与==的区别
- C# equals与==的区别
- c# equals与==的区别
- c# equals与==的区别
- c# equals与==的区别
- Java中"=="与equals()方法的区别
- Java中"=="与equals()方法的区别
- 使用 GNU Libtool 创建库
- 集合框架之collection中接口的用法
- List集合共性方法讲解
- asp.net文件下载,实现隐藏文件下载地址
- make the label text wrap in the gridview
- 关于C#中==与Equals方法的区别总结
- poj 1580 String Matching
- VC6.0中float(单精度)在内存中的存储格式
- android反编译工具整合
- Proteus:logic contentions detected on net B15
- (杯子倒水问题)Pots(p3414)
- “啄木鸟社区”的奋起宣言,
- 很不错的文章
- android 线程