{"record":{"id":"ae3f7a812785aa1b","repo":"TheAlgorithms/C-Sharp","slug":"argumentnullexception-vertices","errorCode":null,"errorMessage":"ArgumentNullException: vertices","messagePattern":"ArgumentNullException: vertices","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Algorithms/Graph/Bridges.cs","lineNumber":26,"sourceCode":"/// Finds bridges (cut edges) in an undirected graph.\n/// A bridge is an edge whose removal increases the number of connected components.\n/// </summary>\npublic static class Bridges\n{\n    /// <summary>\n    /// Finds all bridges in an undirected graph.\n    /// </summary>\n    /// <typeparam name=\"T\">Type of vertex.</typeparam>\n    /// <param name=\"vertices\">All vertices in the graph.</param>\n    /// <param name=\"getNeighbors\">Function to get neighbors of a vertex.</param>\n    /// <returns>Set of bridges as tuples of vertices.</returns>\n    public static HashSet<(T From, T To)> Find<T>(\n        IEnumerable<T> vertices,\n        Func<T, IEnumerable<T>> getNeighbors) where T : notnull\n    {\n        if (vertices == null)\n        {\n            throw new ArgumentNullException(nameof(vertices));\n        }\n\n        if (getNeighbors == null)\n        {\n            throw new ArgumentNullException(nameof(getNeighbors));\n        }\n\n        var vertexList = vertices.ToList();\n        if (vertexList.Count == 0)\n        {\n            return new HashSet<(T, T)>();\n        }\n\n        var bridges = new HashSet<(T, T)>();\n        var visited = new HashSet<T>();\n        var discoveryTime = new Dictionary<T, int>();\n        var low = new Dictionary<T, int>();\n        var parent = new Dictionary<T, T?>();","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Graph/Bridges.cs#L8-L44","documentation":"Bridges.Find computes all bridges in an undirected graph from a vertex collection and a neighbor function. The library throws ArgumentNullException for 'vertices' when the caller passes a null vertex enumerable, because the algorithm cannot enumerate a null sequence. It is an eager argument guard at the start of the public Find method (Algorithms/Graph/Bridges.cs:26).","triggerScenarios":"Calling Algorithms.Graph.Bridges.Find<T>(vertices, getNeighbors) with vertices == null, e.g. Find<int>(null, v => graph[v]).","commonSituations":"Passing a dictionary lookup result that is null (TryGetValue failed), a factory method returning null instead of an empty collection, or wiring up a graph variable that was never initialized before computing bridges.","solutions":["Pass a non-empty or empty collection instead of null (e.g. ?? Enumerable.Empty<T>()).","Fix the upstream source that produced a null vertex set (check TryGetValue / failed lookups).","Wrap the call in a null check or throw a more descriptive exception in your own code before calling Find."],"exampleFix":"// before\nvar bridges = Bridges.Find(adjacencyMap?[nodeKey], n => adjacencyMap[n]);\n// after\nvar vertices = adjacencyMap?[nodeKey] ?? Enumerable.Empty<string>();\nvar bridges = Bridges.Find(vertices, n => adjacencyMap[n]);","handlingStrategy":"validation","validationCode":"if (vertices is null)\n    throw new ArgumentException(\"Vertex collection must not be null\", nameof(vertices));\nvar bridges = Bridges.Find(vertices, getNeighbors);","typeGuard":"static bool HasVertices<T>(IEnumerable<T>? source) => source is not null;","tryCatchPattern":"try\n{\n    var bridges = Bridges.Find(vertices, getNeighbors);\n}\ncatch (ArgumentNullException ex)\n{\n    // ex.ParamName == \"vertices\": fix the null vertex source before retrying\n    logger.LogError(ex, \"Vertex collection was null when computing bridges\");\n}","preventionTips":["Never let graph vertex sources come from nullable lookups without coalescing to Enumerable.Empty<T>().","Enable C# nullable reference types so null vertex arguments are caught at compile time.","Validate graph inputs at the boundary where the graph is loaded, not deep in algorithm calls."],"tags":["null-argument","csharp","graph-algorithms"],"backgroundTag":"null-argument","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}