Contract.ForAll Yöntem

Tanım

Aşırı Yüklemeler

ForAll(Int32, Int32, Predicate<Int32>)

Belirli bir koşulun belirtilen aralıktaki tüm tamsayılar için geçerli olup olmadığını belirler.

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

Koleksiyondaki tüm öğelerin bir işlev içinde bulunup bulunmadığını belirler.

ForAll(Int32, Int32, Predicate<Int32>)

Kaynak:
Contracts.cs
Kaynak:
Contracts.cs
Kaynak:
Contracts.cs

Belirli bir koşulun belirtilen aralıktaki tüm tamsayılar için geçerli olup olmadığını belirler.

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

Parametreler

fromInclusive
Int32

öğesine geçirecek predicateilk tamsayı.

toExclusive
Int32

öğesine geçirecek predicateson tamsayıdan bir fazla.

predicate
Predicate<Int32>

Belirtilen aralıktaki tamsayıların varlığı için değerlendirilecek işlev.

Döndürülenler

true- predicate 1 ile fromInclusivetoExclusive başlayan tüm tamsayılar için döndürürtrue.

Özel durumlar

predicate, null değeridir.

toExclusive değerinden küçüktür fromInclusive.

Örnekler

Aşağıdaki örnekte, bir dizinin null öğesi olup olmadığını belirlemek için yönteminin nasıl kullanılacağı ForAll gösterilmektedir.

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

Açıklamalar

toExclusive parametresi, 0'dan başlayan bir tamsayı aralığının uzunluğunu kullanmayı kolaylaştırmak için son tamsayıdan bir fazladır. Örneğin, 0 ile 4 arasında tamsayılar için 5 olarak ayarlanır.

Ayrıca bkz.

Şunlara uygulanır

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

Kaynak:
Contracts.cs
Kaynak:
Contracts.cs
Kaynak:
Contracts.cs

Koleksiyondaki tüm öğelerin bir işlev içinde bulunup bulunmadığını belirler.

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ür Parametreleri

T

içinde collectionbulunan tür.

Parametreler

collection
IEnumerable<T>

türündeki T öğelerin geçirilecek predicateşekilde çekileceği koleksiyon.

predicate
Predicate<T>

içindeki tüm öğelerin collectionvarlığını değerlendirmek için işlevi.

Döndürülenler

true if and only if predicate returns true for all elements of type T in collection.

Özel durumlar

collection veya predicate şeklindedir null.

Örnekler

Aşağıdaki örnekte, bir koleksiyonun ForAll null öğesi olup olmadığını belirlemek için yönteminin nasıl kullanılacağı gösterilmektedir.

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

Ayrıca bkz.

Şunlara uygulanır