{"record":{"id":"04b172fc22b26243","repo":"TheAlgorithms/C-Sharp","slug":"nameof-bitmapwidth-should-be-greater-than-zero","errorCode":null,"errorMessage":"{nameof(bitmapWidth)} should be greater than zero","messagePattern":"(.+?) should be greater than zero","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Algorithms/Other/KochSnowflake.cs","lineNumber":58,"sourceCode":"        }\n\n        return vectors;\n    }\n\n    /// <summary>\n    ///     Method to render the Koch snowflake to a bitmap. To save the\n    ///     bitmap the command 'GetKochSnowflake().Save(\"KochSnowflake.png\")' can be used.\n    /// </summary>\n    /// <param name=\"bitmapWidth\">The width of the rendered bitmap.</param>\n    /// <param name=\"steps\">The number of iterations.</param>\n    /// <returns>The bitmap of the rendered Koch snowflake.</returns>\n    public static SKBitmap GetKochSnowflake(\n        int bitmapWidth = 600,\n        int steps = 5)\n    {\n        if (bitmapWidth <= 0)\n        {\n            throw new ArgumentOutOfRangeException(\n                nameof(bitmapWidth),\n                $\"{nameof(bitmapWidth)} should be greater than zero\");\n        }\n\n        var offsetX = bitmapWidth / 10f;\n        var offsetY = bitmapWidth / 3.7f;\n        var vector1 = new Vector2(offsetX, offsetY);\n        var vector2 = new Vector2(bitmapWidth / 2, (float)Math.Sin(Math.PI / 3) * bitmapWidth * 0.8f + offsetY);\n        var vector3 = new Vector2(bitmapWidth - offsetX, offsetY);\n        List<Vector2> initialVectors = [vector1, vector2, vector3, vector1];\n        List<Vector2> vectors = Iterate(initialVectors, steps);\n        return GetBitmap(vectors, bitmapWidth, bitmapWidth);\n    }\n\n    /// <summary>\n    ///     Loops through each pair of adjacent vectors. Each line between two adjacent\n    ///     vectors is divided into 4 segments by adding 3 additional vectors in-between\n    ///     the original two vectors. The vector in the middle is constructed through a","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Other/KochSnowflake.cs#L40-L76","documentation":"GetKochSnowflake draws a Koch snowflake fractal into an SKBitmap of the requested width. The library validates that bitmapWidth is strictly positive before allocating and computing offsets, throwing ArgumentOutOfRangeException otherwise. A non-positive width cannot be used to create a bitmap, so the throw is an early fail-fast guard.","triggerScenarios":"Calling Algorithms.Other.KochSnowflake.GetKochSnowflake with bitmapWidth <= 0, e.g. GetKochSnowflake(0) or GetKochSnowflake(-100), or passing a computed width that evaluated to zero or a negative number.","commonSituations":"Widths read from config files, command-line args, or UI inputs that were not validated; integer division or subtraction upstream producing 0; callers passing 0 as a default placeholder meaning 'unset'.","solutions":["Pass a bitmapWidth > 0, e.g. the default 600","Validate the width at the call site before invoking GetKochSnowflake","If the value comes from config or user input, clamp or reject non-positive values where it is parsed"],"exampleFix":"// before\nvar bitmap = KochSnowflake.GetKochSnowflake(userWidth);\n// after\nif (userWidth <= 0) userWidth = 600;\nvar bitmap = KochSnowflake.GetKochSnowflake(userWidth);","handlingStrategy":"validation","validationCode":"if (bitmapWidth <= 0) throw new ArgumentException(\"bitmapWidth must be positive\", nameof(bitmapWidth));","typeGuard":"static bool IsValidBitmapWidth(int w) => w > 0;","tryCatchPattern":"try { var bmp = KochSnowflake.GetKochSnowflake(width); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"bitmapWidth\")\n{\n    // fallback: use default width 600\n}","preventionTips":["Validate widths at config/UI parse time, not at the API call","Never use 0 as an 'unset' sentinel for dimensions","Add unit tests covering boundary widths (0, 1, negative)"],"tags":["argument-out-of-range","fractal","bitmap","csharp"],"backgroundTag":"argument-out-of-range","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"}