I suggest you ...

Integrate Code Contract Keywords into the main .Net Languages

Being able to write Code Contract Keywords would be much more useful than having to write the well-known Rules in the syntax of Statements.
This is especially true for interfaces where we have to construct artificial classes just as a vehicle to be able to specify Code Contract Rules.
After all Code Contracts are rules, not code.

51 votes
Vote
Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
Reiner shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

Thanks for taking the time to share this suggestion. This item has been around for a couple of versions of Visual Studio and we haven’t acted on it. Looking at the VS “15” plans, we’re not going to take action on this item, so we’re going to close it. If the suggestion is still relevant, please either take a look to see if there’s another suggestion that’s similar that you can vote on, or open a new suggestion.

- The Visual Studio Team

3 comments

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...
  • liero commented  ·   ·  Flag as inappropriate

    I vote for either integrating code contracts into language itself or at least native support by the build tools.

  • Justin Minnaar commented  ·   ·  Flag as inappropriate

    Code Contracts in C#

    I personally feel it is time this was embedded into the C# language itself, which I'll consider and forward as a post to the C# team later. This could then simplify the code and make it more readable, perhaps along the lines of

    public class Sample
    {
    public valid string FriendlyList(valid IEnumerable<valid string> values)
    {
    ...
    }
    }

    Where the 'valid' keyword implies not null. Obviously, such language extensions require careful planning and consideration by the team, but would really make code a lot cleaner. This could be extended even to include conditions, such as the following example. (Note that the 0 < result < 5 operation is not valid C# syntax yet but may be in the future, this is a separate suggestion). The reserved word result indicates the value returned from the function.

    public class Sample
    {
    bool ready;
    int progress;

    public decimal Answer
    {
    int a = 1 where (a > 0),
    int b where (b.IsOdd() && b > a)
    }
    where (ready && progress >= 0)
    ensure (progress > 0) && (0 < result < 5)
    {
    ...
    progress++;
    ...
    }
    }

    Also, the language can support invariants on classes

    public class Sample
    ensures (progress >= 0)
    {
    int progress = 0;
    }

    Perhaps we might need to support invariants for use on specific methods

    public class Sample
    ensures (progress >= 1) if ready
    {
    bool ready;
    int progress = 0;

    public void Configure
    {
    progress = 1;
    ready = true;
    }

    public void DoSomething
    requires ready
    {
    }
    }

    This would be a lot cleaner when putting contracts onto interfaces too as we would not need to create an abstract concrete class for these contracts.

Feedback and Knowledge Base