首页 \ 问答 \ 为什么HashTable.Contains()只是在传递null时才返回false?(Why doesn't HashTable.Contains() just simply return false if it is passed a null?)

为什么HashTable.Contains()只是在传递null时才返回false?(Why doesn't HashTable.Contains() just simply return false if it is passed a null?)

我理解为什么将null传递给HashTable.Contains()不起作用,但是我不明白抛出ArgumentNullException是什么意思 - 而不仅仅是返回false ? 抛出异常有什么好处(除了让我在调用.Contains()之前进行空检查)?

由[System.ArgumentNullException]引起
键不能为空。
参数名称:key
在System.Collections.Hashtable.ContainsKey(对象键)
在System.Collections.Hashtable.Contains(对象键)


I understand why passing a null to HashTable.Contains() doesn't work, but I don't understand what the point of it throwing an ArgumentNullException is - instead of just simply returning false? What is the benefit of throwing the exception (other than to make me do null checks before calling .Contains())?

Caused By [System.ArgumentNullException]
Key cannot be null.
Parameter name: key
at System.Collections.Hashtable.ContainsKey(Object key)
at System.Collections.Hashtable.Contains(Object key)


原文:https://stackoverflow.com/questions/3053703
更新时间:2024-02-16 12:02

最满意答案

您无法在网址的哈希上对chrome扩展进行模式匹配。 请参阅Chrome扩展程序:与Gmail邮件匹配网址格式的内容脚本 。 但是你可以做的是检查你正在寻找的术语的document.location.hash属性,并只在那里执行你的脚本。 然后,您可以为站点创建更广泛的模式匹配,但只在需要时才执行站点的功能。

注意如果它必须以#test结束,那么你可以通过添加endsWith函数来简化事情。

就像是:

String.prototype.endsWith = function(suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
};

if(document.location.hash.endsWith("#test")){ /* do stuff */ }

endsWith在JavaScript中取自endsWith


You can't do pattern matching for chrome extensions on the hash of a url. See Chrome extension: Content script matching URL pattern for Gmail message. But what you can do is check the document.location.hash property for the term you are looking for and only execute your script there. Then you can have a broader pattern match for the site but only execute the functionality of the site when required.

NB If it's a hard requirement that it has to end with #test then you can simplify things by adding an endsWith function.

Something like:

String.prototype.endsWith = function(suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
};

if(document.location.hash.endsWith("#test")){ /* do stuff */ }

endsWith taken from endsWith in JavaScript

相关问答

更多
  • 你需要发布或链接到你的整个manifest.json ,但最可能的问题是$(window).load( ... 默认情况下,Chrome内容脚本会在不可预测的时间触发,这可能是在window.onload触发之前或之后! 因此,在您的清单中添加"run_at": "document_end" ,以保证您的脚本将在可预测的时间运行,并在window.onload之前运行。 ~~~ 接下来, 其他扩展可能会阻止该警报 。 如果需要,请检查并禁用其他扩展程序。 ~~~ 最后,可能是YouTube通过AJAX加载了 ...
  • 以下模式意味着内容脚本将被注入到所有页面中: "matches": [ "" ] 您可以在文档中找到详细信息 The following pattern means that content script will be injected into all pages: "matches": [ "" ] You can find details in the docs
  • 您无法在网址的哈希上对chrome扩展进行模式匹配。 请参阅Chrome扩展程序:与Gmail邮件匹配网址格式的内容脚本 。 但是你可以做的是检查你正在寻找的术语的document.location.hash属性,并只在那里执行你的脚本。 然后,您可以为站点创建更广泛的模式匹配,但只在需要时才执行站点的功能。 注意如果它必须以#test结束,那么你可以通过添加endsWith函数来简化事情。 就像是: String.prototype.endsWith = function(suffix) { re ...
  • 您可以通过设置matches: ""来执行此操作matches: ""并使用include_globs指令设置为*://foo.*/*将其*://foo.*/* 。 但是 ,请注意它可能不会完全符合您的要求。 以下是一些与您的glob模式匹配的URL *://foo.*/* : http://foo.com/bar http://foo.google.com/ http://foo.any.domain.that.startswith.that.subdomain.c ...
  • 您似乎对将请求发送到随机域所需的内容存在误解。 CSP指令script-src指示您可以通过