看到以上的代码,应该对非零基数组的创建有一个大致了解,接下来具体看一下Ensures()方法的底层代码:
1234 public static void Ensures(bool condition)
{
AssertMustUseRewriter(ContractFailureKind.Postcondition, "Ensures");
}
1234567891011121314151617181920212223242526 [SecuritySafeCritical]
static partial void AssertMustUseRewriter(ContractFailureKind kind, String contractKind)
{
if (_assertingMustUseRewriter)
System.Diagnostics.Assert.Fail("Asserting that we must use the rewriter went reentrant.", "Didn't rewrite this mscorlib?");
_assertingMustUseRewriter = true;
Assembly thisAssembly = typeof(Contract).Assembly;
StackTrace stack = new StackTrace();
Assembly probablyNotRewritten = null;
for (int i = 0; i < stack.FrameCount; i++)
{
Assembly caller = stack.GetFrame(i).GetMethod().DeclaringType.Assembly;
if (caller != thisAssembly)
{
probablyNotRewritten = caller;
break;
}
}
if (probablyNotRewritten == null)
probablyNotRewritten = thisAssembly;
String simpleName = probablyNotRewritten.GetName().Name;
System.Runtime.CompilerServices.ContractHelper.TriggerFailure(kind, Environment.GetResourceString("MustUseCCRewrite", contractKind, simpleName), null, null, null);
_assertingMustUseRewriter = false;
}
有关非零基数组的创建方法就不做深入的介绍,如果需要使用,可以根据提供的方法重载选择对应的版本实现。