开发者

Hashtable A equals HashtAble B control

开发者 https://www.devze.com 2023-02-09 06:40 出处:网络
Hallo in c# i gottwo hashtable object of which key/value pair is same and i wantto check if that two hashtable key/value pairs are equal..

Hallo in c# i got two hashtable object of which key/value pair is same and i want to check if that two hashtable key/value pairs are equal..

i开发者_如何学运维 tried up hashtable's equal method but not worked

should i check all items with foreach?

thanks


What you want to do is take a set union and see if the size is the same as the count. You could do a set difference but then you'd have to do it both ways. These can easily be done with Linq extension methods, but since you're using Hashtable you have to use Cast() to get it to IEnumerable:

        var table1 = new Hashtable {{"A", 1}, {"B", 2}, {"C", 3}};
        var table2 = new Hashtable {{"B", 2}, {"A", 1}, {"C", 3}};

        bool same = table1.Cast<DictionaryEntry>().Union(table2.Cast<DictionaryEntry>()).Count() == table1.Count;

        Console.WriteLine("Same = " + same);

I typically recommend Dictionary over Hashtable to get the type safety, but the Cast<>() lets you use the Linq stuff just find with the old Hashtable.


i tried up hashtable's equal method but not worked

This will do a reference comparison to see if the two references to hashtables are the same. That is not what you want.

shoul i check all items with foreach?

Yes. Check that the two hashtables have the same number of items, and that all the key/value pairs in the first are in the second.

0

精彩评论

暂无评论...
验证码 换一张
取 消