Show / Hide Table of Contents

    Class Validator

    Helper class with various condition validation utilities.

    Inheritance
    Object
    Validator
    Inherited Members
    Object.ToString()
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.ReferenceEquals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Namespace: Microsoft.Graph.Communications.Common
    Assembly: Microsoft.Graph.Communications.Common.dll
    Syntax
    public static class Validator

    Methods

    Any(IEnumerable)

    Determines whether a sequence contains any elements.

    Declaration
    public static bool Any(this IEnumerable source)
    Parameters
    Type Name Description
    IEnumerable source

    The IEnumerable to check for emptiness.

    Returns
    Type Description
    Boolean

    true if the source sequence contains any elements; otherwise, false.

    Equals<T>(T, T, IComparer, String, String)

    Throw if value> is not equal to expected

    Declaration
    public static T Equals<T>(T value, T expected, IComparer comparer, string paramName = null, string message = null)
    Parameters
    Type Name Description
    T value

    value

    T expected

    expected

    IComparer comparer

    comparer

    String paramName

    paramName

    String message

    Optional message.

    Returns
    Type Description
    T

    The input value if validation succeeded.

    Type Parameters
    Name Description
    T

    Type of the input value

    Exceptions
    Type Condition
    ArgumentException

    The argument value should match the expected value.

    Equals<T>(T, T, String, String)

    Throw if value> is not equal to expected

    Declaration
    public static T Equals<T>(this T value, T expected, string paramName = null, string message = null)
    Parameters
    Type Name Description
    T value

    value

    T expected

    expected

    String paramName

    paramName

    String message

    Optional message.

    Returns
    Type Description
    T

    The input value if validation succeeded.

    Type Parameters
    Name Description
    T

    Type of the input value

    Exceptions
    Type Condition
    ArgumentException

    The argument value should match the expected value.

    Equals<T, TE>(T, T, String)

    Throws the TE if value> is not equal to expected

    Declaration
    public static T Equals<T, TE>(this T value, T expected, string message = null)
    
        where TE : Exception
    Parameters
    Type Name Description
    T value

    value

    T expected

    expected

    String message

    Optional message.

    Returns
    Type Description
    T

    The input value if validation succeeded.

    Type Parameters
    Name Description
    T

    Type of the input value

    TE

    The type of Exception to throw

    FirstObject(IEnumerable)

    Gets the first object in the enumerable.

    Declaration
    public static object FirstObject(this IEnumerable source)
    Parameters
    Type Name Description
    IEnumerable source

    The IEnumerable to check for object.

    Returns
    Type Description
    Object

    langword_csharp_object if the source sequence contains an element; otherwise, null.

    FirstObject(IEnumerable, Func<Object, Boolean>)

    Gets the first object in the enumerable that matches the specified predicate.

    Declaration
    public static object FirstObject(this IEnumerable source, Func<object, bool> predicate)
    Parameters
    Type Name Description
    IEnumerable source

    The IEnumerable to check for object.

    Func<Object, Boolean> predicate

    The Func<T,TResult> used to match the object.

    Returns
    Type Description
    Object

    langword_csharp_object if the source sequence contains the matching element; otherwise, null.

    InRange<T>(T, T, T, String)

    Throws ArgumentOutOfRangeException if value is not in the given range defined by minValue and maxValue

    Declaration
    public static void InRange<T>(this T value, T minValue, T maxValue, string paramName = null)
    
        where T : IComparable<T>
    Parameters
    Type Name Description
    T value

    value to verify

    T minValue

    Minimum range value

    T maxValue

    Maximum range value

    String paramName

    Variable name.

    Type Parameters
    Name Description
    T

    Comparable

    Exceptions
    Type Condition
    ArgumentOutOfRangeException

    If the value is out of range.

    IsEnum(Type)

    Throws the ArgumentException if given type is not Enum

    Declaration
    public static void IsEnum(this Type type)
    Parameters
    Type Name Description
    Type type

    The type

    IsFalse(Boolean, String, String)

    Throw ArgumentException condition is not false

    Declaration
    public static void IsFalse(this bool condition, string paramName = null, string message = null)
    Parameters
    Type Name Description
    Boolean condition

    Condition to check.

    String paramName

    Optional variable name.

    String message

    Optional message.

    IsFalse<T>(Boolean, String)

    Throw T exception if condition is not false

    Declaration
    public static void IsFalse<T>(this bool condition, string message = null)
    
        where T : Exception
    Parameters
    Type Name Description
    Boolean condition

    Condition to check.

    String message

    Optional message.

    Type Parameters
    Name Description
    T

    Exception type to raise

    IsNull(Object, String, String)

    Throw ArgumentException if obj is not null.

    Declaration
    public static void IsNull(this object obj, string paramName = null, string message = null)
    Parameters
    Type Name Description
    Object obj

    The object to check.

    String paramName

    Optional variable name.

    String message

    Optional message.

    IsTrue(Boolean, String, String)

    Throw ArgumentException condition is not true

    Declaration
    public static void IsTrue(this bool condition, string paramName = null, string message = null)
    Parameters
    Type Name Description
    Boolean condition

    Condition to check.

    String paramName

    Optional variable name.

    String message

    Optional message.

    IsTrue<T>(Boolean, String)

    Throw T exception if condition is not true

    Declaration
    public static void IsTrue<T>(this bool condition, string message = null)
    
        where T : Exception
    Parameters
    Type Name Description
    Boolean condition

    Condition to check.

    String message

    Optional message.

    Type Parameters
    Name Description
    T

    Exception type to raise

    NotEmpty<T>(T, String, String)

    Throw ArgumentNullException if obj is null.

    Declaration
    public static T NotEmpty<T>(this T obj, string paramName = null, string message = null)
    
        where T : IEnumerable
    Parameters
    Type Name Description
    T obj

    The object to check.

    String paramName

    Variable name.

    String message

    Optional message.

    Returns
    Type Description
    T

    The validated IEnumerable.

    Type Parameters
    Name Description
    T

    Enumerable type

    Exceptions
    Type Condition
    ArgumentNullException

    If the object is null.

    ArgumentException

    If the object is empty.

    NotEquals<T>(T, T, String, String)

    Throw if value> is equal to unexpected

    Declaration
    public static T NotEquals<T>(this T value, T unexpected, string paramName = null, string message = null)
    Parameters
    Type Name Description
    T value

    The value to compare.

    T unexpected

    The unexpected value.

    String paramName

    the parameter name.

    String message

    Optional message.

    Returns
    Type Description
    T

    The input value if validation succeeded.

    Type Parameters
    Name Description
    T

    Type of the input value

    Exceptions
    Type Condition
    ArgumentException

    The argument value should not match the expected value.

    NotNull<T>(T, String, String)

    Throw ArgumentNullException if obj is null.

    Declaration
    public static T NotNull<T>(this T obj, string paramName = null, string message = null)
    Parameters
    Type Name Description
    T obj

    The object.

    String paramName

    Name of the parameter.

    String message

    The message.

    Returns
    Type Description
    T

    The validated object.

    Type Parameters
    Name Description
    T

    The type of the object to validate.

    Exceptions
    Type Condition
    ArgumentException

    If the object is null.

    NotNullOrWhitespace(String, String, String)

    Throws the ArgumentException if string is null or whitespace.

    Declaration
    public static string NotNullOrWhitespace(this string value, string paramName = null, string message = null)
    Parameters
    Type Name Description
    String value

    value to check

    String paramName

    Variable name.

    String message

    Optional message.

    Returns
    Type Description
    String

    The validated String.

    Exceptions
    Type Condition
    ArgumentException

    If the string is null or whitespace.

    VerifyContains<T>(IEnumerable<T>, T)

    Throws the ArgumentException if collection does not contains the specified value

    Declaration
    public static void VerifyContains<T>(this IEnumerable<T> enumerable, T value)
    Parameters
    Type Name Description
    IEnumerable<T> enumerable

    The enumerable.

    T value

    The value.

    Type Parameters
    Name Description
    T

    Type of value

    VerifyContainsKey<TK, TV>(IDictionary<TK, TV>, TK)

    Throws the ArgumentException if collection does not contain the specified key

    Declaration
    public static void VerifyContainsKey<TK, TV>(this IDictionary<TK, TV> dictionary, TK key)
    Parameters
    Type Name Description
    IDictionary<TK, TV> dictionary

    The dictionary.

    TK key

    The key.

    Type Parameters
    Name Description
    TK

    Key type

    TV

    Value type

    VerifyContainsKey<TK, TV>(IReadOnlyDictionary<TK, TV>, TK)

    Throws the ArgumentException if collection does not contain the specified key

    Declaration
    public static void VerifyContainsKey<TK, TV>(this IReadOnlyDictionary<TK, TV> dictionary, TK key)
    Parameters
    Type Name Description
    IReadOnlyDictionary<TK, TV> dictionary

    The dictionary.

    TK key

    The key.

    Type Parameters
    Name Description
    TK

    Key type

    TV

    Value type

    VerifyNotContainsKey<TK, TV>(IDictionary<TK, TV>, TK)

    Throws the ArgumentException if collection contains the specified key

    Declaration
    public static void VerifyNotContainsKey<TK, TV>(this IDictionary<TK, TV> dictionary, TK key)
    Parameters
    Type Name Description
    IDictionary<TK, TV> dictionary

    The dictionary.

    TK key

    The key.

    Type Parameters
    Name Description
    TK

    Key type

    TV

    Value type

    Back to top Copyright (c) Microsoft Corporation. All rights reserved.