Contract.ForAll<T> Method (IEnumerable<T>, Predicate<T>)
.NET Framework (current version)
Determines whether all the elements in a collection exist within a function.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- collection
-
Type:
System.Collections.Generic.IEnumerable<T>
The collection from which elements of type T will be drawn to pass to predicate.
- predicate
-
Type:
System.Predicate<T>
The function to evaluate for the existence of all the elements in collection.
Return Value
Type: System.Booleantrue 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<T> 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; } } } }
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
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
Show: