字符串比较 ==和Equals

本文深入解析了C#中字符串比较的方法,包括==运算符与Equals方法的内部实现原理,并通过反编译工具展示了具体的IL代码。

一:字符串比较一般比较 1.字符串引用的比较 2.字符串值的比较

二:字符串比较常用2种方式 (==和equals)

1.==比较

分析代码:

1 static void Main(string[] args)
2   {
3          string s1 = "abc";
4           string s2 = "abc";
5           var a = s1 == s2;
6    }
View Code

对应IL代码:

 1   .entrypoint
 2   // 代码大小       30 (0x1e)
 3   .maxstack  2
 4   .locals init ([0] string s1,
 5            [1] string s2,
 6            [2] bool a,
 7            [3] bool b)
 8   IL_0000:  nop
 9   IL_0001:  ldstr      "abc"
10   IL_0006:  stloc.0
11   IL_0007:  ldstr      "abc"
12   IL_000c:  stloc.1
13   IL_000d:  ldloc.0
14   IL_000e:  ldloc.1
15   IL_000f:  call       bool [mscorlib]System.String::op_Equality(string,
16                                                                  string)
View Code

 

内部调用 op_Equality 方法,查看op_Equality  IL代码

 1 .method public hidebysig specialname static 
 2         bool  op_Equality(string a,
 3                           string b) cil managed
 4 {
 5   .custom instance void System.Runtime.TargetedPatchingOptOutAttribute::.ctor(string) = ( 01 00 3B 50 65 72 66 6F 72 6D 61 6E 63 65 20 63   // ..;Performance c
 6                                                                                           72 69 74 69 63 61 6C 20 74 6F 20 69 6E 6C 69 6E   // ritical to inlin
 7                                                                                           65 20 61 63 72 6F 73 73 20 4E 47 65 6E 20 69 6D   // e across NGen im
 8                                                                                           61 67 65 20 62 6F 75 6E 64 61 72 69 65 73 00 00 ) // age boundaries..
 9   // 代码大小       8 (0x8)
10   .maxstack  8
11   IL_0000:  ldarg.0
12   IL_0001:  ldarg.1
13   IL_0002:  call       bool System.String::Equals(string,
14                                                   string)
15   IL_0007:  ret
16 } // end of method String::op_Equality
View Code

我们可以看到op_Equality方法内部调用的是Equals方法,也就是说,只要使用==来比较两个字符串,最终调用的还是Equals方法,二者是等价的。

通过反编译工具查看 op_Equality

1 [__DynamicallyInvokable]
2 public static bool operator ==(string a, string b)
3 {
4     return Equals(a, b);
5 }
6 
7  
View Code

内部实现Equals方法

 1 [__DynamicallyInvokable]
 2 public static bool Equals(string a, string b)
 3 {
 4     if (a == b)
 5     {
 6         return true;
 7     }
 8     if ((a == null) || (b == null))
 9     {
10         return false;
11     }
12     if (a.Length != b.Length)
13     {
14         return false;
15     }
16     return EqualsHelper(a, b);
17 }
18 
19  
20 
21  
22   [__DynamicallyInvokable]
23 public static bool Equals(string a, string b)
24 {
25     if (a == b)
26     {
27         return true;
28     }
29     if ((a == null) || (b == null))
30     {
31         return false;
32     }
33     if (a.Length != b.Length)
34     {
35         return false;
36     }
37     return EqualsHelper(a, b);
38 }
39 
40  
View Code

 

转载于:https://www.cnblogs.com/onlybesile/p/5562897.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值