C#判等对象是否相等的方法汇总
作者:shichen2014 时间:2023-07-13 22:55:07
本文以实例形式展示了C#判等对象是否相等的常用方法,非常实用,可供大家参考借鉴之用。具体分析如下:
一、判断相等的3个方法
1.实例方法
public virtual bool Equals(object obj)
{
return RuntimeHelpers.Equals(this, obj);
}
2.比较值类型静态方法
public static bool Equals(object objA, object objB)
{
return ((objA == objB) || (((objA != null) && (objB != null)) && objA.Equals(objB)));
}
3.比较引用类型静态方法
public static bool ReferenceEquals(object objA, object objB)
{
return (objA == objB);
}
二、判断引用类型是否相等
class Program
{
static void Main(string[] args)
{
Team t1 = new Team("马尔切洛·里皮");
Team t2 = new Team("马尔切洛·里皮");
var result = (t1 == t2);
Console.WriteLine(result);
result = t1.Equals(t2);
Console.WriteLine(result);
Console.ReadKey();
}
}
public class Team
{
public string _coach = string.Empty;
public Team(string coach)
{
this._coach = coach;
}
}
public struct TeamStruct
{
public string _coach;
public TeamStruct(string coach)
{
this._coach = coach;
}
}
运行结果:
false
false
分析:引用类型比较的是引用地址,由于t1和t2指向不同的对象实例,所以dou都返回false。
三、判断值类型是否相等
1.值类型判断方法
派生于System.ValueType,对System.Object中的虚方法Equals(object obj)进行了重写
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
RuntimeType type = (RuntimeType) base.GetType();
RuntimeType type2 = (RuntimeType) obj.GetType();
if (type2 != type) //比较两个对象是否是同一类型
{
return false;
}
object a = this;
if (CanCompareBits(this)) //对象成员如果存在对于堆的引用返回false
{
return FastEqualsCheck(a, obj);
}
//反射获取值类型的所有字段
FieldInfo[] fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
for (int i = 0; i < fields.Length; i++) //遍历字段,对各个字段进行比较
{
object obj3 = ((RtFieldInfo) fields[i]).UnsafeGetValue(a);
object obj4 = ((RtFieldInfo) fields[i]).UnsafeGetValue(obj);
if (obj3 == null)
{
if (obj4 != null)
{
return false;
}
}
else if (!obj3.Equals(obj4))
{
return false;
}
}
return true;
}
2.用==判断是否相等
static void Main(string[] args)
{
TeamStruct ts1 = new TeamStruct("马尔切洛·里皮");
TeamStruct ts2 = ts1;
var result = (ts1 == ts2);
Console.WriteLine(result);
Console.ReadKey();
}
出现编译错误。原因是值类型不能用==进行判断。
3.用Equals()实例方法判断是否相等
static void Main(string[] args)
{
TeamStruct ts1 = new TeamStruct("马尔切洛·里皮");
TeamStruct ts2 = ts1;
var result = ts1.Equals(ts2);
Console.WriteLine(result);
Console.ReadKey();
}
返回true。
可见,如果值类型的字段相等,那就相等。
static void Main(string[] args)
{
TeamStruct ts1 = new TeamStruct("马尔切洛·里皮");
TeamStruct ts2 = ts1;
ts2._coach = "高洪波";
var result = ts1.Equals(ts2);
Console.WriteLine(result);
Console.ReadKey();
}
返回false,当然,值类型的字段有不相等,就会整个不相等。
4.判断复杂值类型是否相等
即值类型中包含引用类型和值类型。
class Program
{
static void Main(string[] args)
{
Team t = new Team("马尔切洛·里皮");
TeamStruct ts = new TeamStruct("马尔切洛·里皮");
NationalTeam nt1 = new NationalTeam(t, ts);
NationalTeam nt2 = nt1;
var result = nt1.Equals(nt2);
Console.WriteLine(result);
Console.ReadKey();
}
}
public class Team
{
public string _coach = string.Empty;
public Team(string coach)
{
this._coach = coach;
}
}
public struct TeamStruct
{
public string _coach;
public TeamStruct(string coach)
{
this._coach = coach;
}
}
public struct NationalTeam
{
public Team _team;
public TeamStruct _structTeam;
public NationalTeam(Team team, TeamStruct structTeam)
{
this._team = team;
this._structTeam = structTeam;
}
}
返回true,会遍历比较引用类型成员和值类型成员。在nt1和nt2中,类型为Team的引用类型成员_team指向同一个对象实例, 类型为TeamStruct的值类型成员_structTeam相等,所有整个返回。如下图所示:
标签:C#
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java基础-Java的体系结构
2022-05-18 00:19:05
![](https://img.aspxhome.com/file/2023/1/63501_0s.png)
C#备忘录人生存档的设计模式实例
2022-06-15 12:40:38
![](https://img.aspxhome.com/file/2023/5/92895_0s.png)
Thymeleaf 3.0 自定义标签方言属性的实例讲解
2023-03-24 20:40:23
MyBatis字段名和属性名不一致的解决方法
2022-12-15 18:15:22
c#基于WinForm的Socket实现简单的聊天室 IM
2021-11-27 04:47:57
![](https://img.aspxhome.com/file/2023/6/81066_0s.png)
SpringBoot访问外部文件及默认路由问题
2021-08-12 10:58:01
![](https://img.aspxhome.com/file/2023/7/65157_0s.png)
完美解决SpringCloud-OpenFeign使用okhttp替换不生效问题
2023-07-03 11:27:25
![](https://img.aspxhome.com/file/2023/3/83443_0s.jpg)
Flutter添加页面过渡动画实现步骤
2023-08-20 09:06:20
![](https://img.aspxhome.com/file/2023/1/94991_0s.png)
C#中如何使用Chart图表问题
2023-04-02 16:35:06
![](https://img.aspxhome.com/file/2023/9/104229_0s.png)
C# 屏蔽关键字的实现方法
2023-10-10 15:15:50
![](https://img.aspxhome.com/file/2023/7/91387_0s.png)
Java字符串编码知识点详解介绍
2023-10-16 09:41:04
java8 stream自定义分组求和并排序的实现
2022-09-12 04:08:26
![](https://img.aspxhome.com/file/2023/5/79175_0s.png)
Android开发使用Messenger及Handler进行通信的方法示例
2022-02-23 23:35:22
横竖屏切换导致页面频繁重启screenLayout解析
2021-06-14 05:16:22
![](https://img.aspxhome.com/file/2023/0/137470_0s.png)
Android自定义View实现炫酷进度条
2023-09-22 00:16:07
![](https://img.aspxhome.com/file/2023/3/126753_0s.jpg)
Java递归寻路实现,你真的理解了吗
2022-09-17 02:24:34
![](https://img.aspxhome.com/file/2023/5/69105_0s.png)
IntelliJ IDEA 2022.2 正式发布新功能体验
2021-08-14 02:47:33
![](https://img.aspxhome.com/file/2023/7/95947_0s.png)
Java字节码中jvm实例用法
2023-08-08 05:25:09
C#实现显示CPU使用率与内存使用率
2022-09-26 15:03:58
![](https://img.aspxhome.com/file/2023/5/113695_0s.png)
Centos中安装jdk案例讲解
2023-04-30 00:37:50
![](https://img.aspxhome.com/file/2023/5/74975_0s.png)