Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7087799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:43:07+00:00 2026-05-28T07:43:07+00:00

I have just started using CodeContracts with C#, and I might need some help.

  • 0

I have just started using CodeContracts with C#, and I might need some help. I have a class with a static list of keys, and I have a static method called Remove() that removes a key from that list.

Now what I understand from the contracts post conditions is that as the programmer of the class, I guarantee that the current public method does something specific, in this case : removing the element of the list.

Here’s what I’ve written:

    private static List<Keys> m_usedKeys; // private list of keys

    public static void Remove(Keys _key)
    {
        m_usedKeys.Remove(_key);
        Contract.Ensures(!m_usedKeys.Any(x => x == _key));
    }

What I’m trying to “Ensure” here, is that the Remove() method removes the key for real from the list (Yes this might be a trivial example, but I get the error there.)

when I write the Contract.Ensures() line, VS2010 gives me the following error :

Error   3   Detected expression statement evaluated for potential side-effect in contracts of method 'LibJungleTimer.KeyBind.Remove(System.Windows.Forms.Keys)'. (Did you mean to put the expression into a Requires, Ensures, or Invariant call?)  C:\Users\Joel\Documents\Programmation\JT\JungleTimer\LibJungleTimer\KeyBind.cs  51

I think this error says that the expression m_usedKeys.remove(_key); has a potential side-effect. In fact, it has a side-effect, it removes a key from the list!

If I try to write something like this instead :

Contract.Assert(!m_usedKeys.Any(x => x == _key));

well it compiles fine. My question is what am I doing wrong? Shouldn’t CodeContracts be used like that to ensure a post condition?

EDIT: Yes I indeed meant !m_usedKeys.Any(…);

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:43:08+00:00Added an answer on May 28, 2026 at 7:43 am

    The Code Contracts tool assumes that anything before the last Contract.Ensures / Contract.Requires is part of the contract… so it thinks your call to Remove is part of the contract, not part of the implementation. You have to reverse your code, so that all the contract code is before the implementation.

    public static void Remove(Keys _key)
    {
        Contract.Ensures(m_usedKeys.Any(x => x == _key));        
        m_usedKeys.Remove(_key);
    }
    

    I suspect you want !m_usedKeys.Any(...) by the way… and I’d also be wary of this code to start with, given that you’ve got no threading protection in place. It wouldn’t bother me if this were an instance method, but static methods accessing static (i.e. global) state should generally be thread-safe.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started using Qt and noticed that all the example class definitions have
please could someone help me please as have just started using actionscript and really
I have just started using OutputCache on some of my controller actions and I
I have just started using OutputCache on some of my controller actions and I
I have just started using SlickGrid and was hoping someone can help me solve
I have just started using RadControls so this question might be basic for you,
I have just started using Tkinter for a programming class and am having a
I have just started using jqGrid , and I have some tables that I
I have just started using PowerShell today, and I have an intention list files
I just started using QT and I have a question regarding some basic stuff.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.