Export (0) Print
Expand All

Contract.ForAll Method (Int32, Int32, Predicate<Int32>)

Determines whether a particular condition is valid for all integers in a specified range.

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

public static bool ForAll(
	int fromInclusive,
	int toExclusive,
	Predicate<int> predicate
)

Parameters

fromInclusive
Type: System.Int32

The first integer to pass to predicate.

toExclusive
Type: System.Int32

One more than the last integer to pass to predicate.

predicate
Type: System.Predicate<Int32>

The function to evaluate for the existence of the integers in the specified range.

Return Value

Type: System.Boolean
true if predicate returns true for all integers starting from fromInclusive to toExclusive - 1.

ExceptionCondition
ArgumentNullException

predicate is null.

ArgumentException

toExclusive is less than fromInclusive.

The toExclusive parameter is one more than the last integer to facilitate using the length of a range of integers starting at 0. For example, it would be set to 5 for integers 0 through 4.

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;
            }
        }
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft