Contract.ForAll メソッド

定義

オーバーロード

ForAll(Int32, Int32, Predicate<Int32>)

指定した範囲内のすべての整数について特定の状態が有効かどうかを判定します。Determines whether a particular condition is valid for all integers in a specified range.

ForAll<T>(IEnumerable<T>, Predicate<T>)

コレクション内のすべての要素が関数内に存在するかどうかを判断します。Determines whether all the elements in a collection exist within a function.

ForAll(Int32, Int32, Predicate<Int32>)

指定した範囲内のすべての整数について特定の状態が有効かどうかを判定します。Determines whether a particular condition is valid for all integers in a specified range.

public:
 static bool ForAll(int fromInclusive, int toExclusive, Predicate<int> ^ predicate);
public static bool ForAll (int fromInclusive, int toExclusive, Predicate<int> predicate);
static member ForAll : int * int * Predicate<int> -> bool
Public Shared Function ForAll (fromInclusive As Integer, toExclusive As Integer, predicate As Predicate(Of Integer)) As Boolean

パラメーター

fromInclusive
Int32

predicate に渡す最初の整数。The first integer to pass to predicate.

toExclusive
Int32

predicate に渡す最後の整数より 1 だけ大きい数。One more than the last integer to pass to predicate.

predicate
Predicate<Int32>

指定された範囲の整数の存在を評価する関数。The function to evaluate for the existence of the integers in the specified range.

戻り値

true から predicate - 1 の範囲のすべての整数に対して、truefromInclusive を返す場合は toExclusivetrue if predicate returns true for all integers starting from fromInclusive to toExclusive - 1.

例外

predicatenull です。predicate is null.

toExclusivefromInclusive より小さい値です。toExclusive is less than fromInclusive.

次の例は、ForAll メソッドを使用して、配列に null 要素があるかどうかを判断する方法を示しています。The following example demonstrates how to use the ForAll method to determine whether an array has a null element.

using System;
using System.Diagnostics.Contracts;
using System.Collections.Generic;
namespace AssumeEx
{
    class Program
    {
        // Start application with at least two arguments
        static void Main(string[] args)
        {
            args[1] = null;
            Contract.Requires(args != null && Contract.ForAll(0, args.Length, i => args[i] != null));
            // test the ForAll method.  This is only for purpose of demonstrating how ForAll works.
            CheckIndexes(args);
            Stack<string> numbers = new Stack<string>();
            numbers.Push("one");
            numbers.Push("two");
            numbers.Push(null);
            numbers.Push("four");
            numbers.Push("five");
            Contract.Requires(numbers != null && !Contract.ForAll(numbers, (String x) => x != null));
            // test the ForAll generic overload.  This is only for purpose of demonstrating how ForAll works.
            CheckTypeArray(numbers);
        }

        private static bool CheckIndexes(string[] args)
        {
            try
            {
                if (args != null && !Contract.ForAll(0, args.Length, i => args[i] != null))
                    throw new ArgumentException("The parameter array has a null element", "args");
                return true;
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
        }
        private static bool CheckTypeArray(IEnumerable<String> xs)
        {
            try
            {
                if (xs != null && !Contract.ForAll(xs, (String x) => x != null))
                    throw new ArgumentException("The parameter array has a null element", "indexes");
                return true;
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
        }
    }
}
Imports System.Diagnostics.Contracts
Imports System.Collections.Generic


Class Program

    ' Start application with at least two arguments.
    Shared Sub Main(ByVal args() As String)
        args(1) = Nothing
        Contract.Requires(Not (args Is Nothing) AndAlso Contract.ForAll(args, Function(s) s Is Nothing))
        ' test the ForAll method.  This is only for purpose of demonstrating how ForAll works.
        CheckIndexes(args)
        Dim numbers As New Stack(Of String)
        numbers.Push("one")
        numbers.Push("two")
        numbers.Push("three")
        numbers.Push("four")
        numbers.Push("five")

        Contract.Requires(Not (numbers Is Nothing) AndAlso Not Contract.ForAll(numbers, Function(s) s Is Nothing))
        ' test the ForAll generic overload. This is only for purpose of demonstrating how ForAll works.
        CheckTypeArray(numbers)

    End Sub


    Private Shared Function CheckIndexes(ByVal args() As String) As Boolean
        Try
            If Not (args Is Nothing) AndAlso Not Contract.ForAll(0, args.Length, Function(i) args(i) Is Nothing) Then
                Throw New ArgumentException("The parameter array has a null element", "args")
            End If
            Return True
        Catch e As ArgumentException
            Console.WriteLine(e.Message)
            Return False
        End Try

    End Function 'CheckIndexes

    Private Shared Function CheckTypeArray(ByVal xs As Stack(Of String)) As Boolean

        Try
            If Not (xs Is Nothing) AndAlso Not Contract.ForAll(xs, Function(s) s Is Nothing) Then

                Throw New ArgumentException("The parameter array has a null element", "Stack")
            End If
            Return True

        Catch e As ArgumentException
            Console.WriteLine(e.Message)
            Return False
        End Try

    End Function 'CheckTypeArray
End Class

注釈

toExclusive パラメーターは、0から始まる整数の範囲の長さを使用しやすくするために、最後の整数より1つ大きい値です。The toExclusive parameter is one more than the last integer to facilitate using the length of a range of integers starting at 0. たとえば、整数 0 ~ 4 の場合は5に設定されます。For example, it would be set to 5 for integers 0 through 4.

こちらもご覧ください

ForAll<T>(IEnumerable<T>, Predicate<T>)

コレクション内のすべての要素が関数内に存在するかどうかを判断します。Determines whether all the elements in a collection exist within a function.

public:
generic <typename T>
 static bool ForAll(System::Collections::Generic::IEnumerable<T> ^ collection, Predicate<T> ^ predicate);
public static bool ForAll<T> (System.Collections.Generic.IEnumerable<T> collection, Predicate<T> predicate);
static member ForAll : seq<'T> * Predicate<'T> -> bool
Public Shared Function ForAll(Of T) (collection As IEnumerable(Of T), predicate As Predicate(Of T)) As Boolean

型パラメーター

T

collection に格納されている型。The type that is contained in collection.

パラメーター

collection
IEnumerable<T>

に渡される型 Tpredicate の要素が含まれているコレクション。The collection from which elements of type T will be drawn to pass to predicate.

predicate
Predicate<T>

collection 内のすべての要素の存在を評価する関数。The function to evaluate for the existence of all the elements in collection.

戻り値

true 内の型 predicate のすべての要素に対して、trueT を返す場合にのみ collectiontrue if and only if predicate returns true for all elements of type T in collection.

例外

collection または predicatenull です。collection or predicate is null.

次の例では、ForAll メソッドを使用して、コレクションに null 要素が含まれているかどうかを確認する方法を示します。The following example demonstrates how to use the ForAll method to determine whether a collection has a null element.

using System;
using System.Diagnostics.Contracts;
using System.Collections.Generic;
namespace AssumeEx
{
    class Program
    {
        // Start application with at least two arguments
        static void Main(string[] args)
        {
            args[1] = null;
            Contract.Requires(args != null && Contract.ForAll(0, args.Length, i => args[i] != null));
            // test the ForAll method.  This is only for purpose of demonstrating how ForAll works.
            CheckIndexes(args);
            Stack<string> numbers = new Stack<string>();
            numbers.Push("one");
            numbers.Push("two");
            numbers.Push(null);
            numbers.Push("four");
            numbers.Push("five");
            Contract.Requires(numbers != null && !Contract.ForAll(numbers, (String x) => x != null));
            // test the ForAll generic overload.  This is only for purpose of demonstrating how ForAll works.
            CheckTypeArray(numbers);
        }

        private static bool CheckIndexes(string[] args)
        {
            try
            {
                if (args != null && !Contract.ForAll(0, args.Length, i => args[i] != null))
                    throw new ArgumentException("The parameter array has a null element", "args");
                return true;
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
        }
        private static bool CheckTypeArray(IEnumerable<String> xs)
        {
            try
            {
                if (xs != null && !Contract.ForAll(xs, (String x) => x != null))
                    throw new ArgumentException("The parameter array has a null element", "indexes");
                return true;
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
        }
    }
}
Imports System.Diagnostics.Contracts
Imports System.Collections.Generic


Class Program

    ' Start application with at least two arguments.
    Shared Sub Main(ByVal args() As String)
        args(1) = Nothing
        Contract.Requires(Not (args Is Nothing) AndAlso Contract.ForAll(args, Function(s) s Is Nothing))
        ' test the ForAll method.  This is only for purpose of demonstrating how ForAll works.
        CheckIndexes(args)
        Dim numbers As New Stack(Of String)
        numbers.Push("one")
        numbers.Push("two")
        numbers.Push("three")
        numbers.Push("four")
        numbers.Push("five")

        Contract.Requires(Not (numbers Is Nothing) AndAlso Not Contract.ForAll(numbers, Function(s) s Is Nothing))
        ' test the ForAll generic overload. This is only for purpose of demonstrating how ForAll works.
        CheckTypeArray(numbers)

    End Sub


    Private Shared Function CheckIndexes(ByVal args() As String) As Boolean
        Try
            If Not (args Is Nothing) AndAlso Not Contract.ForAll(0, args.Length, Function(i) args(i) Is Nothing) Then
                Throw New ArgumentException("The parameter array has a null element", "args")
            End If
            Return True
        Catch e As ArgumentException
            Console.WriteLine(e.Message)
            Return False
        End Try

    End Function 'CheckIndexes

    Private Shared Function CheckTypeArray(ByVal xs As Stack(Of String)) As Boolean

        Try
            If Not (xs Is Nothing) AndAlso Not Contract.ForAll(xs, Function(s) s Is Nothing) Then

                Throw New ArgumentException("The parameter array has a null element", "Stack")
            End If
            Return True

        Catch e As ArgumentException
            Console.WriteLine(e.Message)
            Return False
        End Try

    End Function 'CheckTypeArray
End Class

こちらもご覧ください

適用対象