代码跳过 Contract.Requires
我正在尝试使用 c# 合约编写此方法...但是在调试时,它完全忽略了 Contract.requires 和 CheckRep() 我是否错误地使用了它?
public Poly Add(Poly q)
{
CheckRep();
Contract.Requires(q != null, "You need to provide a valid non-null Poly.");
Poly la, sm;
if (deg > q.deg)
{
la = this; sm = q;
}
else
{
la = q; sm = this;
}
int newdeg = la.deg;
if (deg == q.deg)
{
for (int k = deg; k > 0; k--)
{
if (trms[k] + q.trms[k] != 0)
{
break;
}
else
{
newdeg--;
}
}
}
Poly r = new Poly(newdeg);
int i;
for (i = 0; i <= sm.deg && i <= newdeg; i++)
{
r.trms[i] = sm.trms[i] + la.trms[i];
}
for (int j = i; j <= newdeg; j++)
{
r.trms[j] = la.trms[j];
}
return r;
}
I'm trying to write this method using c# contracts...but when debugging, it completely ignores the Contract.requires and CheckRep() Am I using this incorrectly??
public Poly Add(Poly q)
{
CheckRep();
Contract.Requires(q != null, "You need to provide a valid non-null Poly.");
Poly la, sm;
if (deg > q.deg)
{
la = this; sm = q;
}
else
{
la = q; sm = this;
}
int newdeg = la.deg;
if (deg == q.deg)
{
for (int k = deg; k > 0; k--)
{
if (trms[k] + q.trms[k] != 0)
{
break;
}
else
{
newdeg--;
}
}
}
Poly r = new Poly(newdeg);
int i;
for (i = 0; i <= sm.deg && i <= newdeg; i++)
{
r.trms[i] = sm.trms[i] + la.trms[i];
}
for (int j = i; j <= newdeg; j++)
{
r.trms[j] = la.trms[j];
}
return r;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它必须是:
来自MSDN:
此方法调用必须位于方法或属性的开头,任何其他代码之前。
该合同向客户公开;因此,它必须仅引用至少与封闭方法一样可见的成员。
您还必须激活运行时检查。右键单击您的项目->属性。单击左侧菜单中的“代码合同”。选中“执行运行时联系检查”
It has to be:
From MSDN:
This method call must be at the beginning of a method or property, before any other code.
This contract is exposed to clients; therefore, it must only reference members that are at least as visible as the enclosing method.
You also have to activate runtime checking. Right click on your project->properties. Click "Code Contracts" in the left hand menu. Check "Perfrom Runtime Contact Checking"