AvaloniaUI/Avalonia · error · ArgumentException

Invalid value

Error message

Invalid value

What it means

Thrown by the IterationCount(ulong, IterationType) constructor when the type argument is an invalid enum value greater than IterationType.Infinite. IterationType is { Many=0, Infinite=1 }, so any other numeric value is rejected.

Source

Thrown at src/Avalonia.Base/Animation/IterationCount.cs:44

        /// <summary>
        /// Initializes a new instance of the <see cref="IterationCount"/> struct.
        /// </summary>
        /// <param name="value">The number of iterations of an animation.</param>
        public IterationCount(ulong value)
            : this(value, IterationType.Many)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="IterationCount"/> struct.
        /// </summary>
        /// <param name="value">The size of the IterationCount.</param>
        /// <param name="type">The unit of the IterationCount.</param>
        public IterationCount(ulong value, IterationType type)
        {
            if (type > IterationType.Infinite)
            {
                throw new ArgumentException("Invalid value", nameof(type));
            }

            _type = type;
            _value = value;
        }

        /// <summary>
        /// Gets an instance of <see cref="IterationCount"/> that indicates that an animation
        /// should repeat forever.
        /// </summary>
        public static IterationCount Infinite => new IterationCount(0, IterationType.Infinite);

        /// <summary>
        /// Gets the unit of the <see cref="IterationCount"/>.
        /// </summary>
        public IterationType RepeatType => _type;

        /// <summary>

View on GitHub (pinned to 11c5427268)

Solutions

  1. Use only IterationType.Many or IterationType.Infinite.
  2. Validate the enum with Enum.IsDefined before constructing.
  3. Prefer IterationCount.Infinite or new IterationCount(n) helpers over the two-arg constructor.

Example fix

// before
var ic = new IterationCount(3, (IterationType)5);

// after
var ic = new IterationCount(3, IterationType.Many);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(IterationType), type))
    throw new ArgumentException("Invalid IterationType.", nameof(type));

Type guard

static bool IsValidIterationType(IterationType t) => Enum.IsDefined(typeof(IterationType), t);

Prevention

When it happens

Trigger: Constructing new IterationCount(value, (IterationType)99) or otherwise casting an out-of-range integer to IterationType.

Common situations: Casting an arbitrary int to IterationType without validation; deserializing a numeric enum value outside the defined range.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/e38bb809a9f00782. Report an issue: GitHub.