本文整理匯總了C#中System.Diagnostics.Contracts.Contract.ForAll方法的典型用法代碼示例。如果您正苦於以下問題:C# Contract.ForAll方法的具體用法?C# Contract.ForAll怎麽用?C# Contract.ForAll使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。
在下文中一共展示了Contract.ForAll方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
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;
}
}
}
}