{"record":{"id":"4fca05ca1773d608","repo":"louthy/language-ext","slug":"notsupportedexception-temperature","errorCode":null,"errorMessage":"NotSupportedException","messagePattern":"NotSupportedException","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Units of Measure/Temperature.cs","lineNumber":74,"sourceCode":"    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    static double FtoC(double x) => (x - 32.0) * 5.0 / 9.0;\n\n    public override int GetHashCode() =>\n        Value.GetHashCode();\n\n    public override bool Equals(object? obj) =>\n        obj is Temperature t && Equals(t);\n\n    public bool Equals(Temperature rhs) =>\n        Value.Equals(rhs.Value);\n\n    public override string ToString() =>\n        Type switch\n        {\n            UnitType.K => $\"{Value} K\",\n            UnitType.C => $\"{Value} °C\",\n            UnitType.F => $\"{Value} °F\",\n            _          => throw new NotSupportedException(Type.ToString())\n        };\n\n    public Temperature Kelvin =>\n        Type switch\n        {\n            UnitType.K => this,\n            UnitType.C => new Temperature(UnitType.K, CtoK(Value)),\n            UnitType.F => new Temperature(UnitType.K, FtoK(Value)),\n            _          => throw new NotSupportedException(Type.ToString())\n        };\n\n    public double KValue =>\n        Type switch\n        {\n            UnitType.K => Value,\n            UnitType.C => CtoK(Value),\n            UnitType.F => FtoK(Value),\n            _          => throw new NotSupportedException(Type.ToString())","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Units of Measure/Temperature.cs#L56-L92","documentation":"Temperature.ToString() switches on the internal UnitType (K/C/F) and throws NotSupportedException when the unit field holds an undefined value. In normal use UnitType is always one of the three enum members, so this is a defensive guard against a corrupted or default-initialized Temperature whose Type field was set outside the enum's valid range.","triggerScenarios":"Calling ToString() on a Temperature whose UnitType was created from an invalid cast, e.g. new Temperature((UnitType)7, 100) via internal/reflection paths, or a default(Temperature)-like state produced through unsafe initialization.","commonSituations":"Deserialization tools that set fields directly without validation; reflection-based test harnesses; casting arbitrary ints to UnitType and calling the internal constructor.","solutions":["Only construct Temperature through the public FromCelcius/FromFahrenheit/FromKelvin factories.","Check UnitType is K, C, or F before operations on reflectively created instances.","Fix the code that assigns an out-of-range UnitType instead of handling it at ToString.","Catch NotSupportedException around formatting if inputs come from untrusted deserialization."],"exampleFix":"// before\nvar t = new Temperature((UnitType)99, 20); // ToString -> NotSupportedException\n// after\nvar t = UnitType.C == unit ? Temperature.FromCelcius(20) : Temperature.FromKelvin(293.15);","handlingStrategy":"type-guard","validationCode":"bool ok = Enum.IsDefined(typeof(UnitType), t.Type);","typeGuard":"static bool HasValidUnit(Temperature t) => t.Type is UnitType.K or UnitType.C or UnitType.F;","tryCatchPattern":"try { s = t.ToString(); }\ncatch (NotSupportedException ex) { /* invalid UnitType on instance */ }","preventionTips":["Build Temperature only via FromCelcius/FromFahrenheit/FromKelvin","Never cast raw ints to UnitType","Validate enum fields after deserialization"],"tags":["not-supported-exception","units-of-measure","temperature","enum"],"backgroundTag":"invalid-enum-value","analyzedSha":"2f0e3628242889774d4141960a35671a0280051f","analyzedAt":"2026-09-15T03:31:55.716Z","contentChangedAt":"2026-09-15T03:31:55.716Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}