< Summary

Information
Class: AmbientServices.Utilities.EnumUtilities
Assembly: AmbientServices
File(s): /home/runner/work/AmbientServices/AmbientServices/AmbientServices/Utilities/EnumExtensions.cs
Tag: 332_35464845198
Line coverage
100%
Covered lines: 1
Uncovered lines: 0
Coverable lines: 1
Total lines: 43
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MaxEnumValue<T>()100%11100%

File(s)

/home/runner/work/AmbientServices/AmbientServices/AmbientServices/Utilities/EnumExtensions.cs

#LineLine coverage
 1using System;
 2using System.Linq;
 3
 4namespace AmbientServices.Utilities;
 5
 6/// <summary>
 7/// A static class that holds extensions to the system <see cref="Enum"/> class.
 8/// </summary>
 9/// <remarks>
 10/// <pitch>The largest defined value of an enum type without paying reflection cost on every call.</pitch>
 11/// <plan>Delegates to <see cref="EnumMax{T}"/>, whose static initializer enumerates the enum's defined values once per 
 12/// </remarks>
 13internal static class EnumUtilities
 14{
 15    /// <summary>
 16    /// Returns the highest possible value for an enum.
 17    /// </summary>
 18    /// <typeparam name="T">The enum to get the maximum value for.</typeparam>
 19    /// <returns>The highest enum value.</returns>
 20    public static T MaxEnumValue<T>() where T : Enum
 21    {
 222        return EnumMax<T>.Max;
 23    }
 24}
 25
 26/// <summary>
 27/// A static class that holds onto the computed max enum value.
 28/// </summary>
 29/// <typeparam name="T"></typeparam>
 30/// <remarks>
 31/// <pitch>The per-enum-type cache backing <see cref="EnumUtilities.MaxEnumValue{T}"/>; the CLR's generic static initial
 32/// </remarks>
 33internal static class EnumMax<T> where T : Enum
 34{
 35    private static T Init()
 36    {
 37        Array a = Enum.GetValues(typeof(T))!;   // I don't think it's possible to have a System.Enum for which Enum.GetV
 38        return a.Length == 0
 39            ? default!                          // apparently the compiler isn't smart enough to know that even though S
 40            : a.Cast<T>().Max()!;
 41    }
 42    public static T Max { get; } = Init();
 43}

Methods/Properties

MaxEnumValue<T>()