比较哈希表中的值 [英] Comparing values in a hash table

查看:120
本文介绍了比较哈希表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我需要检查哈希表的密钥并比较它的值。我认为以下代码可行:



Hi everyone.

I have a requirement to check the key of a hashtable and compare it's value. I thought the following code would have worked:

private void Startup()
       {
           GetSettings(); // Get the test program settings from the text file.
           if (hsh_Settings.ContainsKey("TestMode") == true)
           {
               if (hsh_Settings["TestMode"] == "Production")
               {
                    SetProductionMode();
               }
               if (hsh_Settings["TestMode"] == "Engineer")
               {
                    SetEngineeringMode();
               }
           }
           else
           {
               SetProductionMode();
           }
       }





不幸的是我收到了以下警告: 可能的意外参考比较;得到一个值比较,左侧输入'string'



为了让这个警告消失,我做了问:





Unfortunately I received the following warning: Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string'

To make this warning go away, I did as asked:

private void Startup()
       {
           GetSettings(); // Get the test program settings from the text file.
           if (hsh_Settings.ContainsKey("TestMode") == true)
           {
               if ((string)hsh_Settings["TestMode"] == "Production")
               {
                    SetProductionMode();
               }
               if ((string)hsh_Settings["TestMode"] == "Engineer")
               {
                    SetEngineeringMode();
               }
           }
           else
           {
               SetProductionMode();
           }
       }







现在没有警告并且代码有效。我的问题是我没有正确理解演员要求的原因。也许有一种更简洁的方法可以比较不需要进行混乱投射的散列表值?



提前谢谢,



Brian




Now there are no warnings and the code works. My problem is that I don't properly understand the reason for the cast requirement. Perhaps there is a cleaner way of comparing hashtable values that does not require messy casting to be done?

Thanks in advance,

Brian

推荐答案

是的,有更清洁的方法。实际上,您的解决方案从一开始就很脏:您使用的是非泛型哈希表,它本身就基于类型转换。换句话说,编译时元素类型是 System.Object ,因此如果存储其他类型的对象,则需要进行类型转换(仍然是对象但具有不同的运行时类型)。



所有这些集合(专业集合除外)早已被淘汰.NET v.2.0,当引入泛型时。没有正式标记 System.ObsoleteAttribute 只是因为在旧版软件单元中使用它没有任何问题,为了兼容性,但在新开发的软件中使用它们毫无意义,永远。



在你的情况下,你应该使用类型 System.Collections.Generic.Dictionary< string,string>

http://msdn.microsoft.com/en-us /library/xfhwa508.aspx [ ^ ]。



参见: http://msdn.microsoft.com/en-us/library/System.Collections.Generic.aspx [ ^ ]。



非泛型非特别收藏品已经消失了,不要不再使用它们了。







我看到你仍然对投。您需要了解如何比较字符串以及如何完成引用类型对象之间的默认比较。通常,比较参考。但是你可以重新定义比较方法 System.Object.Equals(System.Object),以某种不同的方式比较对象,比如说,语义上。如果这样做,您可以选择==比较(可以单独重新定义)和比较,它总是使用 System.Object.ReferenceEquals 检查引用标识。请参阅:

http://msdn.microsoft.com/en -us / library / system.object.aspx [ ^ ]。



编译器不知道一个操作数是一个字符串并比较引用,这不是你想要做的。对于字符串,它会比较字符串内容。 System.String 是一个非常类似于语义值的引用类。它比大多数人想象的要复杂得多。有关理解,请参阅:

http:// msdn。 microsoft.com/en-us/library/system.string.intern.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.string.isinterned.aspx [ ^ ],

http://en.wikipedia.org/wiki/String_intern_pool [ ^ ]。



您真的需要了解参考和值类型以及使用引用。这些概念是.NET和CLI的基础。



-SA
Yes, there is much cleaner approach. Actually, your solution is pretty dirty from the very beginning: you are using a non-generic hash table which inherently based on type casting. In other words, compile-time element type is System.Object, so you need to do type casts if you store objects of other types (which are still objects but have different run-time types).

All such collections (except specialized ones) have been rendered obsolete as early as of .NET v.2.0, when generics were introduced. The were not formally marked with System.ObsoleteAttribute just because there is nothing wrong with using it in legacy software units, for compatibility, but using them in newly developed software makes no sense, ever.

In your case, you should use the type System.Collections.Generic.Dictionary<string, string>:
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx[^].

See also: http://msdn.microsoft.com/en-us/library/System.Collections.Generic.aspx[^].

Non-generic non-specialized collections have gone, don't use them anymore.



As I see you are still confused with the cast. You need to understand how strings are compared and how default comparison between reference-type objects is done. Normally, references are compared. But you can redefine comparison method System.Object.Equals(System.Object), to compare object in some different way, let's say, semantically. If this is done, you have a choice between "==" comparison (which can be redefined separately) and comparison which always checks referential identity, using System.Object.ReferenceEquals. Please see:
http://msdn.microsoft.com/en-us/library/system.object.aspx[^].

Compiler does not "know" that one operand is a string and compares references, which is not what you meant to do. For strings, it compares string contents. System.String is a reference class which closely mimic value semantic. It is much more complex then most people think. For understanding, please see:
http://msdn.microsoft.com/en-us/library/system.string.intern.aspx[^],
http://msdn.microsoft.com/en-us/library/system.string.isinterned.aspx[^],
http://en.wikipedia.org/wiki/String_intern_pool[^].

You really need to understand reference and value types and working with references. These concepts are fundamental to .NET and CLI.

—SA


请仔细阅读链接

http://stackoverflow.com/questions/12263715/possible-unintended - 参考比较 [ ^ ]

希望这有助于
Please go through this link
http://stackoverflow.com/questions/12263715/possible-unintended-reference-comparison[^]
Hope this helps


这篇关于比较哈希表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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