如何检测C#中是否存在此字典键?

编程入门 行业动态 更新时间:2024-10-19 06:21:09
本文介绍了如何检测C#中是否存在此字典键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用带有联系数据的Exchange Web服务托管API。我有以下代码,它们是功能,但不理想:

I am working with the Exchange Web Services Managed API, with contact data. I have the following code, which is functional, but not ideal:

foreach (Contact c in contactList) { string openItemUrl = "" + service.Url.Host + "/owa/" + c.WebClientReadFormQueryString; row = table.NewRow(); row["FileAs"] = c.FileAs; row["GivenName"] = c.GivenName; row["Surname"] = c.Surname; row["CompanyName"] = c.CompanyName; row["Link"] = openItemUrl; //home address try { row["HomeStreet"] = c.PhysicalAddresses[PhysicalAddressKey.Home].Street.ToString(); } catch (Exception e) { } try { row["HomeCity"] = c.PhysicalAddresses[PhysicalAddressKey.Home].City.ToString(); } catch (Exception e) { } try { row["HomeState"] = c.PhysicalAddresses[PhysicalAddressKey.Home].State.ToString(); } catch (Exception e) { } try { row["HomeZip"] = c.PhysicalAddresses[PhysicalAddressKey.Home].PostalCode.ToString(); } catch (Exception e) { } try { row["HomeCountry"] = c.PhysicalAddresses[PhysicalAddressKey.Home].CountryOrRegion.ToString(); } catch (Exception e) { } //and so on for all kinds of other contact-related fields... }

我说过,这段代码有效。现在,如果可能的话,我想让它吸一些 。

As I said, this code works. Now I want to make it suck a little less, if possible.

我找不到任何方法可以检查在尝试访问它之前,字典中是否存在该键,如果我尝试读取它(使用 .ToString()),但它不存在,则出现异常抛出:

I can't find any methods that allow me to check for the existence of the key in the dictionary before attempting to access it, and if I try to read it (with .ToString()) and it doesn't exist then an exception is thrown:

500 字典中没有给定的键。

500 The given key was not present in the dictionary.

我该如何重构此代码以减少吸吮(同时仍然起作用)?

How can I refactor this code to suck less (while still being functional)?

推荐答案

您可以使用 ContainsKey :

You can use ContainsKey:

if (dict.ContainsKey(key)) { ... }

或 TryGetValue :

dict.TryGetValue(key, out value);

更新:根据发表评论,这里的实际课程不是 IDictionary ,而是 PhysicalAddressDictionary ,因此方法为 包含 和 TryGetValue ,但是它们的工作方式相同。

Update: according to a comment the actual class here is not an IDictionary but a PhysicalAddressDictionary, so the methods are Contains and TryGetValue but they work in the same way.

示例用法:

PhysicalAddressEntry entry; PhysicalAddressKey key = c.PhysicalAddresses[PhysicalAddressKey.Home].Street; if (c.PhysicalAddresses.TryGetValue(key, out entry)) { row["HomeStreet"] = entry; }

更新2 :这是工作代码(由提问者编译)

Update 2: here is the working code (compiled by question asker)

PhysicalAddressEntry entry; PhysicalAddressKey key = PhysicalAddressKey.Home; if (c.PhysicalAddresses.TryGetValue(key, out entry)) { if (entry.Street != null) { row["HomeStreet"] = entry.Street.ToString(); } }

...根据需要重复内部条件每个所需的密钥。对于每个PhysicalAddressKey(家庭,工作等),只完成一次TryGetValue。

...with the inner conditional repeated as necessary for each key required. The TryGetValue is only done once per PhysicalAddressKey (Home, Work, etc).

更多推荐

如何检测C#中是否存在此字典键?

本文发布于:2023-11-06 14:08:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563905.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是否存在   字典

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!