Contract.ForAll 方法

定义

重载

ForAll(Int32, Int32, Predicate<Int32>)

确定某个特定条件是否对指定范围内的所有整数都有效。

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

确定函数中是否存在某个集合中的所有元素。

ForAll(Int32, Int32, Predicate<Int32>)

Source:
Contracts.cs
Source:
Contracts.cs
Source:
Contracts.cs

确定某个特定条件是否对指定范围内的所有整数都有效。

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 的第一个整数。

toExclusive
Int32

要传递给 predicate 的最后一个整数加一。

predicate
Predicate<Int32>

要计算其中是否存在指定范围内的整数的函数。

返回

如果 predicate 对于从 fromInclusive 开始到 toExclusive - 1 的范围内的所有整数都返回 true,则为 true

例外

predicatenull

toExclusive 小于 fromInclusive

示例

以下示例演示如何使用 ForAll 方法确定数组是否具有 null 元素。

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 开始的整数范围的长度。 例如,对于整数 0 到 4,它将设置为 5。

另请参阅

适用于

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

Source:
Contracts.cs
Source:
Contracts.cs
Source:
Contracts.cs

确定函数中是否存在某个集合中的所有元素。

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 中包含的类型。

参数

collection
IEnumerable<T>

将从中绘制要传递给 的类型的 T 元素的 predicate集合。

predicate
Predicate<T>

用于计算 collection 中所有元素是否存在的函数。

返回

当且仅当 predicate 对于 T 中的 collection 类型的所有元素都返回 true 时,才为 true

例外

collectionpredicatenull

示例

以下示例演示如何使用 ForAll 方法确定集合是否具有 null 元素。

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

另请参阅

适用于