{"record":{"id":"88ebf39276271f41","repo":"TheAlgorithms/C-Sharp","slug":"value-cannot-be-null-parameter-collection","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'collection')","messagePattern":"Value cannot be null\\. \\(Parameter 'collection'\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Algorithms/Search/RecursiveBinarySearcher.cs","lineNumber":23,"sourceCode":"/// </summary>\n/// <typeparam name=\"T\">Type of searcher target.</typeparam>\npublic class RecursiveBinarySearcher<T> where T : IComparable<T>\n{\n    /// <summary>\n    ///     Finds index of item in collection that equals to item searched for,\n    ///     time complexity: O(log(n)),\n    ///     space complexity: O(1),\n    ///     where n - collection size.\n    /// </summary>\n    /// <param name=\"collection\">Sorted collection to search in.</param>\n    /// <param name=\"item\">Item to search for.</param>\n    /// <exception cref=\"ArgumentNullException\">Thrown if input collection is null.</exception>\n    /// <returns>Index of item that equals to item searched for or -1 if none found.</returns>\n    public int FindIndex(IList<T>? collection, T item)\n    {\n        if (collection is null)\n        {\n            throw new ArgumentNullException(nameof(collection));\n        }\n\n        var leftIndex = 0;\n        var rightIndex = collection.Count - 1;\n\n        return FindIndex(collection, item, leftIndex, rightIndex);\n    }\n\n    /// <summary>\n    ///     Finds index of item in array that equals to item searched for,\n    ///     time complexity: O(log(n)),\n    ///     space complexity: O(1),\n    ///     where n - array size.\n    /// </summary>\n    /// <param name=\"collection\">Sorted array to search in.</param>\n    /// <param name=\"item\">Item to search for.</param>\n    /// <param name=\"leftIndex\">Minimum search range.</param>\n    /// <param name=\"rightIndex\">Maximum search range.</param>","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Search/RecursiveBinarySearcher.cs#L5-L41","documentation":"RecursiveBinarySearcher.FindIndex throws ArgumentNullException with parameter name \"collection\" (via nameof) when the IList<T> input is null. Binary search needs collection.Count and indexer access, so a null collection is rejected before the recursion starts.","triggerScenarios":"Passing a null IList<T> — a List<T> field never assigned, a method returning null instead of an empty list, or an optional collection parameter left unset.","commonSituations":"Repository/service layers returning null collections on miss; model binding producing null lists; test fixtures that skip list initialization.","solutions":["Coalesce to an empty list before the call: collection ?? Array.Empty<T>() (with an IList<T> adapter) or return -1 early when null.","Initialize list properties/fields at construction (new List<T>() or Array.Empty<T>()).","Fix upstream APIs to return empty collections rather than null.","Catch ArgumentNullException if a null collection is a meaningful state in your flow."],"exampleFix":"// before\nreturn binarySearcher.FindIndex(items, value);\n// after\nif (items is null) return -1;\nreturn binarySearcher.FindIndex(items, value);","handlingStrategy":"validation","validationCode":"if (collection is null) return -1;","typeGuard":"bool HasCollection<T>(IList<T>? c) => c is not null;","tryCatchPattern":"try { return binarySearcher.FindIndex(collection, item); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"collection\") { return -1; }","preventionTips":["Initialize IList fields/properties eagerly (new List<T>() or Array.Empty<T>()).","Adopt the 'return empty collection, never null' convention across your codebase.","Enable nullable reference types to surface null flows at compile time.","Guard at service boundaries before calling third-party searchers."],"tags":["null-argument","binary-search","argument-validation"],"backgroundTag":"null-argument","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}