{"record":{"id":"a61a5e4ff630f59b","repo":"SixLabors/ImageSharp","slug":"cannot-use-the-default-value-type-instance-to-dither","errorCode":null,"errorMessage":"Cannot use the default value type instance to dither.","messagePattern":"Cannot use the default value type instance to dither\\.","errorType":"exception","errorClass":"ImageProcessingException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Processing/Processors/Dithering/ErrorDither.cs","lineNumber":240,"sourceCode":"    /// <inheritdoc/>\n    public override bool Equals(object? obj)\n        => obj is ErrorDither dither && this.Equals(dither);\n\n    /// <inheritdoc/>\n    public bool Equals(ErrorDither other)\n        => this.offset == other.offset && this.matrix.Equals(other.matrix);\n\n    /// <inheritdoc/>\n    public bool Equals(IDither? other)\n        => this.Equals((object?)other);\n\n    /// <inheritdoc/>\n    public override int GetHashCode()\n        => HashCode.Combine(this.offset, this.matrix);\n\n    [MethodImpl(InliningOptions.ColdPath)]\n    private static void ThrowDefaultInstance()\n        => throw new ImageProcessingException(\"Cannot use the default value type instance to dither.\");\n}\n","sourceCodeStart":222,"sourceCodeEnd":242,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Processing/Processors/Dithering/ErrorDither.cs#L222-L242","documentation":"ErrorDither is a readonly struct; its default instance has a null/empty matrix, so applying it would read out-of-bounds. ApplyQuantizationDither and ApplyPaletteDither call ThrowDefaultInstance, throwing ImageProcessingException 'Cannot use the default value type instance to dither.'","triggerScenarios":"Declaring an uninitialized field/property of type ErrorDither (default(ErrorDither)) and passing it to e.g. Quantizer options Dither property, or an API accepting IDither where a default ErrorDither was assigned; a struct array element never initialized.","commonSituations":"Using 'default' as a placeholder for optional dithering; new ErrorDither[...] arrays with untouched elements; DI/config binding creating a default instance when the dither name didn't resolve.","solutions":["Use the predefined static instances: ErrorDither.FloydSteinberg, ErrorDither.Burkes, ErrorDither.Atkinson, ErrorDither.Sierra3, ErrorDither.Sierra2, ErrorDither.SierraLite, ErrorDither.Stucki, ErrorDither.JarvisJudiceNinke","If dithering should be optional, pass null (or use IDither and omit it) instead of default(ErrorDither)","Check for default before applying: if (dither == default) throw or skip","Fix config/binding code so the named dither resolves to a known static instance"],"exampleFix":"// before\nIDither dither = default;\noptions.Dither = dither;\n// after\noptions.Dither = ErrorDither.FloydSteinberg; // or null to disable","handlingStrategy":"validation","validationCode":"if (dither is ErrorDither ed && ed == default) throw new InvalidOperationException(\"Dither not initialized\");","typeGuard":"static bool IsInitialized(ErrorDither d) => d != default;","tryCatchPattern":"try { options.Dither = dither; } catch (ImageProcessingException ex) when (ex.Message.Contains(\"default value type instance\")) { options.Dither = ErrorDither.FloydSteinberg; }","preventionTips":["Always initialize ErrorDither from its static instances","Use null instead of default to mean 'no dither'","Initialize all struct-array elements used as dither configs"],"tags":["dithering","quantization","struct-default"],"backgroundTag":"invalid-argument-value","analyzedSha":"59ce6af6fc29027cda277ef62d4d1694a8acce91","analyzedAt":"2026-09-13T18:34:59.331Z","contentChangedAt":"2026-09-13T18:34:59.331Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}