本文整理汇总了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;
}
}
}
}