Contract.ForAll(Of T) Method (IEnumerable(Of T), Predicate(Of T))

 

Determines whether all the elements in a collection exist within a function.

Namespace:   System.Diagnostics.Contracts
Assembly:  mscorlib (in mscorlib.dll)

Public Shared Function ForAll(Of T) (
	collection As IEnumerable(Of T),
	predicate As Predicate(Of T)
) As Boolean

Parameters

collection
Type: System.Collections.Generic.IEnumerable(Of T)

The collection from which elements of type T will be drawn to pass to predicate.

predicate
Type: System.Predicate(Of T)

The function to evaluate for the existence of all the elements in collection.

Return Value

Type: System.Boolean

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

Type Parameters

T

The type that is contained in collection.

Exception Condition
ArgumentNullException

collection or predicate is null.

The following example demonstrates how to use the ForAll(Of T) method to determine whether a collection has a null element.

Imports System
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 'Main


    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 'Program

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show: