Questions tagged «argumentnullexception»

3
为什么此字符串扩展方法不引发异常?
我有一个C#字符串扩展方法,该方法应返回IEnumerable<int>字符串中子字符串的所有索引中的一个。它可以完美地实现其预期目的,并且可以返回预期的结果(由我的一个测试证明,虽然不是以下测试),但是另一个单元测试发现了一个问题:它无法处理空参数。 这是我正在测试的扩展方法: public static IEnumerable<int> AllIndexesOf(this string str, string searchText) { if (searchText == null) { throw new ArgumentNullException("searchText"); } for (int index = 0; ; index += searchText.Length) { index = str.IndexOf(searchText, index); if (index == -1) break; yield return index; } } 这是检测出该问题的测试: [TestMethod] [ExpectedException(typeof(ArgumentNullException))] public void Extensions_AllIndexesOf_HandlesNullArguments() …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.