Contract.ForAll Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Overload
ForAll(Int32, Int32, Predicate<Int32>) |
Menentukan apakah kondisi tertentu valid untuk semua bilangan bulat dalam rentang tertentu. |
ForAll<T>(IEnumerable<T>, Predicate<T>) |
Menentukan apakah semua elemen dalam koleksi ada dalam fungsi. |
ForAll(Int32, Int32, Predicate<Int32>)
- Sumber:
- Contracts.cs
- Sumber:
- Contracts.cs
- Sumber:
- Contracts.cs
Menentukan apakah kondisi tertentu valid untuk semua bilangan bulat dalam rentang tertentu.
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
Parameter
- fromInclusive
- Int32
Bilangan bulat pertama yang diteruskan ke predicate
.
- toExclusive
- Int32
Satu lebih dari bilangan bulat terakhir untuk diteruskan ke predicate
.
Fungsi untuk mengevaluasi keberadaan bilangan bulat dalam rentang yang ditentukan.
Mengembalikan
true
jika predicate
mengembalikan true
untuk semua bilangan bulat mulai dari fromInclusive
hingga toExclusive
- 1.
Pengecualian
predicate
adalah null
.
toExclusive
kurang dari fromInclusive
.
Contoh
Contoh berikut menunjukkan cara menggunakan ForAll metode untuk menentukan apakah array memiliki elemen 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
Keterangan
Parameter toExclusive
adalah satu lebih dari bilangan bulat terakhir untuk memfasilitasi penggunaan panjang rentang bilangan bulat mulai dari 0. Misalnya, akan diatur ke 5 untuk bilangan bulat 0 hingga 4.
Lihat juga
Berlaku untuk
ForAll<T>(IEnumerable<T>, Predicate<T>)
- Sumber:
- Contracts.cs
- Sumber:
- Contracts.cs
- Sumber:
- Contracts.cs
Menentukan apakah semua elemen dalam koleksi ada dalam fungsi.
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
Jenis parameter
- T
Jenis yang terkandung dalam collection
.
Parameter
- collection
- IEnumerable<T>
Koleksi dari elemen jenis T
mana yang akan digambar untuk diteruskan ke predicate
.
- predicate
- Predicate<T>
Fungsi untuk mengevaluasi keberadaan semua elemen di collection
.
Mengembalikan
true
jika dan hanya jika predicate
mengembalikan true
untuk semua elemen jenis T
dalam collection
.
Pengecualian
collection
atau predicate
adalah null
.
Contoh
Contoh berikut menunjukkan cara menggunakan ForAll metode untuk menentukan apakah koleksi memiliki elemen 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