Table of Contents

TypeArgumentOutOfRangeException Class

Definition

Namespace
Cuemon
Assemblies
Cuemon.Kernel.dll
Source
src/Cuemon.Kernel/TypeArgumentOutOfRangeException.cs

The exception that is thrown when the value of an type argument is outside the allowable range of values as defined by the invoked method.

public class TypeArgumentOutOfRangeException : ArgumentOutOfRangeException, ISerializable
Inheritance
TypeArgumentOutOfRangeException
Implements
Inherited Members

Examples

The following example demonstrates how to throw a when an enum-based type argument is outside the valid range.

// Program.cs
using System;
using Cuemon;
using Contoso.Logging;

TypeArgumentOutOfRangeExceptionExample.Run();

namespace Contoso.Logging
{
    public sealed class TypeArgumentOutOfRangeExceptionExample
    {
        public static void Run()
        {
            LogLevel parsed = ParseEnum<LogLevel>("Warning");
            Console.WriteLine(parsed);

            try
            {
                ParseEnum<int>("1");
            }
            catch (TypeArgumentOutOfRangeException ex)
            {
                Console.WriteLine($"{ex.ParamName}: {ex.ActualValue}");
            }
        }

        private static TEnum ParseEnum<TEnum>(string text)
            where TEnum : struct
        {
            if (!typeof(TEnum).IsEnum)
            {
                throw new TypeArgumentOutOfRangeException(
                    nameof(TEnum),
                    typeof(TEnum),
                    "Type arguments must be enum types.");
            }

            return (TEnum)Enum.Parse(typeof(TEnum), text, ignoreCase: true);
        }

        private enum LogLevel
        {
            Debug,
            Information,
            Warning,
            Error
        }
    }
}

Constructors

Name Description
TypeArgumentOutOfRangeException()

Initializes a new instance of the TypeArgumentOutOfRangeException class.

TypeArgumentOutOfRangeException(string)

Initializes a new instance of the TypeArgumentOutOfRangeException class.

TypeArgumentOutOfRangeException(string, Exception)

Initializes a new instance of the TypeArgumentOutOfRangeException class.

TypeArgumentOutOfRangeException(string, object, string)

Initializes a new instance of the TypeArgumentOutOfRangeException class.

TypeArgumentOutOfRangeException(string, string)

Initializes a new instance of the TypeArgumentOutOfRangeException class.