{"record":{"id":"b2f4869a6eebeb3c","repo":"TheAlgorithms/C-Sharp","slug":"the-input-expression-cannot-be-null-or-empty","errorCode":null,"errorMessage":"The input expression cannot be null or empty.","messagePattern":"The input expression cannot be null or empty\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Algorithms/Stack/BalancedParenthesesChecker.cs","lineNumber":35,"sourceCode":"    /// Determines if a given string expression containing brackets is balanced.\n    /// A string is considered balanced if all opening brackets have corresponding closing brackets\n    /// in the correct order. The supported brackets are '()', '{}', and '[]'.\n    /// </summary>\n    /// <param name=\"expression\">\n    /// The input string expression containing the brackets to check for balance.\n    /// </param>\n    /// <returns>\n    /// <c>true</c> if the brackets in the expression are balanced; otherwise, <c>false</c>.\n    /// </returns>\n    /// <exception cref=\"ArgumentException\">\n    /// Thrown when the input expression contains invalid characters or is null/empty.\n    /// Only '(', ')', '{', '}', '[', ']' characters are allowed.\n    /// </exception>\n    public bool IsBalanced(string expression)\n    {\n        if (string.IsNullOrEmpty(expression))\n        {\n            throw new ArgumentException(\"The input expression cannot be null or empty.\");\n        }\n\n        Stack<char> stack = new Stack<char>();\n        foreach (char c in expression)\n        {\n            if (IsOpeningParenthesis(c))\n            {\n                stack.Push(c);\n            }\n            else if (IsClosingParenthesis(c))\n            {\n                if (!IsBalancedClosing(stack, c))\n                {\n                    return false;\n                }\n            }\n            else\n            {","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Stack/BalancedParenthesesChecker.cs#L17-L53","documentation":"BalancedParenthesesChecker.IsBalanced throws ArgumentException(\"The input expression cannot be null or empty.\") when the expression string is null or the empty string. There is nothing to validate in such input, and the library treats it as an invalid argument rather than silently returning true/false.","triggerScenarios":"IsBalanced(null) or IsBalanced(\"\") — passing user input that was never entered, an empty config value, or a string trimmed down to nothing before the call.","commonSituations":"Form fields submitted empty; file/line reads yielding empty strings; optional settings defaulting to null; pipelines where an upstream filter removed all characters (e.g. stripped non-parenthesis chars leaving \"\").","solutions":["Check string.IsNullOrWhiteSpace(expression) before calling and decide the desired semantics (treat empty as balanced = true, or report a validation error).","Sanitize input earlier in the pipeline so the checker only receives non-empty parenthesis strings.","Catch ArgumentException at the boundary and convert to your own validation message for the user.","Provide a sensible default expression when the input source is optional."],"exampleFix":"// before\nbool ok = checker.IsBalanced(userInput);\n// after\nbool ok = string.IsNullOrWhiteSpace(userInput) ? true : checker.IsBalanced(userInput);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(expression)) return true; // or your chosen empty-input semantics","typeGuard":"bool HasExpression(string? s) => !string.IsNullOrWhiteSpace(s);","tryCatchPattern":"try { return checker.IsBalanced(expression); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"null or empty\")) { return true; }","preventionTips":["Trim and check user input before validation calls.","Define explicit empty-input semantics in your own API and document them.","Filter characters upstream only if you re-check for emptiness afterwards.","Give optional inputs a concrete default value instead of null/empty."],"tags":["argument-validation","empty-input","validation"],"backgroundTag":"empty-required-field","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"}