c# - 需要什么来证明本契约(Contract)的要求?

标签 c# .net design-by-contract microsoft-contracts

我有一个在锦标赛中运行的应用程序,并且我收到了有关此简化代码结构的契约(Contract)警告:

    public static void LoadState(IList<Object> stuff)
    {
        for(int i = 0; i < stuff.Count; i++)
        {
            // Contract.Assert(i < stuff.Count);
            // Contract.Assume(i < stuff.Count);

            Object thing = stuff[i];

            Console.WriteLine(thing.ToString());
        }
    }

警告是:

contracts: requires unproven: index < @this.Count

我做错了什么?我如何在 IList<T> 上证明这一点?这是静态分析器中的错误吗?我如何向 Microsoft 提交错误报告?

最佳答案

这看起来确实很奇怪。不幸的是,我使用的是带有代码契约的 VS2010 专业版,因此​​我无法自己运行 cccheck 来玩。

您确实需要索引而不仅仅是使用 foreach 循环吗?

只是为了确定 - 上面的简化示例是否会产生相同的错误?总是值得检查一下简化是否没有消除问题:) 例如,您是否对 stuff 做了任何其他事情,契约(Contract)检查器可能会使用这些来使有关 stuff.Count< 的保证无效

关于c# - 需要什么来证明本契约(Contract)的要求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1230070/

相关文章:

web-services - JSON RESTful Web 服务是否应该使用数据契约

c# - mvc5 身份 AllowAnonymous

c# - 将 Google AMP(Accelerated Mobile Pages)应用于 ASP.NET Core 站点

C# 在不同程序集中定义重写方法

c# - 将 SQL DB 部署到 Azure 超时启用索引

c# - 为什么我的 Html.DropDownList 会生成一个充满 "System.Web.Mvc.SelectListItem"字符串的列表?

c# - 为什么添加Newtonsoft.Json后Team City会出现编译错误?

javascript - <asp :Button> event not firing when within element affected by jQuery

ios - 在 Swift 中按契约(Contract)设计

c# - 代码契约(Contract) : How do I state in a post-condition that a field/property's value has not changed?