.equals vs == C#详细 [英] .equals vs == C# detailed

查看:59
本文介绍了.equals vs == C#详细的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据表dtdata,列内的值是1.

为了避免使用.ToString()我认为使用.Equals



我试过这个



I am having datatable dtdata and the value inside the column is 1.
In order to avoid a .ToString() I thoughtouf using .Equals

I have tried this

if(dtData.Rows[0]["Column1"].ToString() == "1") -- working

if(dtData.Rows[0]["Column1"].ToString().Equals("1"))  ---  working 





但它不能像这样工作





but its not working like this

if(dtData.Rows[0]["Column1"].Equals("1"))  --- not working 



我以为.equals不会检查类型,价值仅对吗?



任何人都可以澄清一下像这样的实际情况而不是理论?



提前谢谢


I thought ".equals" wont be checking type, value only right ?

Can any one please clarify With a practical situation like this and not theories ?

Thanks in advance

推荐答案

这个代码项目文章解释了差异很好==和之间有什么区别。 Equals()? [ ^ ]
This codeproject article explains the differences quite nicely What is the difference between "==" and .Equals()?[^]


if(dtData.Rows[0]["Column1"].Equals(1))





尝试这是否有效,如果是,只是你将字符串与整数进行比较,否则问题出在其他地方。



编辑:

因此经过一些测试后,这就是我得到的:

Integer.Equals()将返回true即使你尝试另一个字节较短的直接类型,例如Short或Byte,只要它们的值相同。

如果你尝试等于字节,则同样适用于Short具有相同的值它返回true,但是如果你尝试等于整数,那么它会更长,它会失败。

但是当你直接使用它时它看起来不适用。



演示



Try if this works, if yes, it was just you comparing string to integer, otherwise problem is elsewhere.


So after some testing this is what I got:
Integer.Equals() will return true even if you try another direct type which is shorter byte-wise, for example Short or Byte, as long as their value is same.
Same goes for Short, if you try to Equal to Byte with same value it returns true, but if you try to Equal to Integer, who is longer it will fail.
But this looks like it doesn't apply when you use value directly.

Demonstration

int i = 1;
short s = 1;
byte b = 1;

Console.WriteLine(i.Equals(1)); // True
Console.WriteLine(s.Equals(1)); // True
Console.WriteLine(b.Equals(1)); // True

Console.WriteLine((1).GetType().ToString()); // System.Int32

// Int.Equals(Short) - Int32.Equals(Int16)
Console.WriteLine(i.Equals(s)); // True

// Short.Equals(Int) - Int16.Equals(Int32)
Console.WriteLine(s.Equals(i)); // False





你遇到的问题是Integer.Equals与Object.Equals不同(或根据MSDN的ValueType.Equals,不确定使用哪个),这也将检查类型和Praveen Kumar Upadhyay评论你的dtData.Rows可能是对象的数组,在尝试了GetType之后,即使它是Int16或Int32,或者它仍将使用Object.Equals而不是它所使用的类型的尊重方法。



注意我检查了数字1是什么类型,它是System.Int32,现在当我们比较我们遇到的类型时。



您所处的情况如下:



The problem you have is that Integer.Equals is not same Object.Equals (or ValueType.Equals according to MSDN, not sure which is used though), which will also check for type and as Praveen Kumar Upadhyay commented your dtData.Rows is probably array of objects, after trying out GetType even if it sais it's Int16 or Int32 or whatever it will still use Object.Equals instead of the respected method of the types it sais it is.

Notice I checked what type a number 1 is, it's System.Int32 and now when we also compare type we run into this.

The situation you are in is like this:

Console.WriteLine(((object)i).Equals(1)); // True
Console.WriteLine(((object)s).Equals(1)); // False
Console.WriteLine(((object)b).Equals(1)); // False





修复它的方法是



And way to fix it is

Console.WriteLine(((object)i).Equals(1)); // True
Console.WriteLine(((object)s).Equals((short)1)); // True
Console.WriteLine(((object)b).Equals((byte)1)); // True


正如您所提到的,您的列类型是tinyint(1)。现在当我们将select语句加载到DataTable中时,tinyint列值将被转换为short类型。



现在我将解释你的场景,为什么
As you have mentioned that your column type is tinyint(1). Now when we load the select statement into DataTable, tinyint column value is getting converted into short type of.

Now I will explain your scenario, why
if(dtData.Rows[0]["Column1"].Equals("1"))

返回false。



考虑下面的例子。



returning false.

Consider the below example.

short s = 1;
s.ToString() == "1"



以上语句将返回 true ,因为当我们正在做s.ToString()返回1时我们将其与1进行比较。






above statement will return true, because when we are doing s.ToString() is returning "1" and we are comparing this with "1".


s.ToString().Equals("1")



以上语句将返回 true ,因为我们在做什么s.ToString()返回1,我们使用Equals将其与1进行比较。




above statement will return true, because when we are doing s.ToString() is returning "1" and we are comparing this with "1" using Equals.

s.Equals("1")



以上陈述将返回 false ,因为我们正在比较两种不同的类型,其中1和等于(1)是不同的内容。




above statement will return false, because we are comparing two different types where 1 and Equals("1") are different content.

s.Equals(1)



以上语句将返回 true ,因为现在我们正在比较同一类型value,表示内容是相同的类型,其中1和Equals(1)是相同的内容/值。





希望这将解决您的查询为什么 if(dtData.Rows [0] [Column1]。等于(1))---不工作


above statement will return true, because now we are comparing the same type value, means contents are same types where 1 and Equals(1) are same content/value.


Hope this will solve your query of why if(dtData.Rows[0]["Column1"].Equals("1")) --- not working


这篇关于.equals vs == C#详细的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆