{"record":{"id":"728a3957be5d18ea","repo":"TheAlgorithms/C-Sharp","slug":"array","errorCode":null,"errorMessage":"array","messagePattern":"array","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Algorithms/Search/FibonacciSearcher.cs","lineNumber":24,"sourceCode":"/// <typeparam name=\"T\">Type of array element.</typeparam>\npublic class FibonacciSearcher<T> where T : IComparable<T>\n{\n    /// <summary>\n    ///     Finds the index of the item searched for in the array.\n    ///     Time complexity:\n    ///     worst-case: O(log n),\n    ///     average-case: O(log n),\n    ///     best-case: O(1).\n    /// </summary>\n    /// <param name=\"array\">Sorted array to be searched in. Cannot be null.</param>\n    /// <param name=\"item\">Item to be searched for. Cannot be null.</param>\n    /// <returns>If an item is found, return index. If the array is empty or an item is not found, return -1.</returns>\n    /// <exception cref=\"ArgumentNullException\">Gets thrown when the given array or item is null.</exception>\n    public int FindIndex(T[] array, T item)\n    {\n        if (array is null)\n        {\n            throw new ArgumentNullException(\"array\");\n        }\n\n        if (item is null)\n        {\n            throw new ArgumentNullException(\"item\");\n        }\n\n        var arrayLength = array.Length;\n\n        if (arrayLength > 0)\n        {\n            // find the smallest Fibonacci number that equals or is greater than the array length\n            var fibonacciNumberBeyondPrevious = 0;\n            var fibonacciNumPrevious = 1;\n            var fibonacciNum = fibonacciNumPrevious;\n\n            while (fibonacciNum <= arrayLength)\n            {","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Search/FibonacciSearcher.cs#L6-L42","documentation":"FibonacciSearcher.FindIndex validates its inputs and throws ArgumentNullException with parameter name \"array\" when the array argument is null. The library requires a non-null array to run the Fibonacci search; null is treated as a programming error, not as 'nothing to search'.","triggerScenarios":"Calling FindIndex(null, item) — e.g. an array field not yet initialized, a factory method returning null, or a deserialization failure upstream.","commonSituations":"Arrays produced by LINQ chains that can return null instead of empty; uninitialized class members; API responses mapped to null arrays; test setups that forgot to instantiate the fixture data.","solutions":["Ensure the array is instantiated before calling FindIndex; initialize fields with Array.Empty<T>() instead of leaving them null.","Add a null check or `?? Array.Empty<T>()` coalesce at the call site before invoking the searcher.","Fix the upstream producer so it returns an empty array rather than null on 'no data'.","If null is legitimate, catch ArgumentNullException and handle it explicitly."],"exampleFix":"// before\nvar idx = searcher.FindIndex(maybeNullArray, item);\n// after\nvar idx = searcher.FindIndex(maybeNullArray ?? Array.Empty<int>(), item);","handlingStrategy":"validation","validationCode":"if (array is null) throw new InvalidOperationException(\"Search array must be initialized before calling FindIndex.\");","typeGuard":"bool HasArray<T>(T[]? array) => array is not null;","tryCatchPattern":"try { return searcher.FindIndex(array, item); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"array\") { return -1; }","preventionTips":["Initialize array fields with Array.Empty<T>() instead of null.","Enable nullable reference types so null-flow is caught at compile time.","Make producers return empty arrays rather than null.","Null-check parameters at public boundaries of your own code before delegating."],"tags":["null-argument","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"}