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

  • Home
  • SEARCH
  • 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 1003625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:00:30+00:00 2026-05-16T08:00:30+00:00

This is my code: public class RegularPolygon { public int VertexCount; public double SideLength;

  • 0

This is my code:

public class RegularPolygon
{
    public int VertexCount;
    public double SideLength;

    public RegularPolygon(int vertexCount, double sideLength)
    {
        Contract.Requires(vertexCount >= 3);
        VertexCount = vertexCount;
        SideLength = sideLength;
    }

    [ContractInvariantMethod]
    private void RegularPolygonInvariants()
    {
        Contract.Invariant(VertexCount>=3);
    }

}

I have tried with both the Contract.Requires and Contract.Invariant methods to prevent the vertexCount variable from becoming less than or equal to 2; however I am still able to initialise a RegularPolygon with 2 or fewer sides. My (simplified) NUnit test looks like this:

[TestFixture]
class TestRegularPolygon
{
    private RegularPolygon _polygon;

    [SetUp]
    public void Init()
    {
        _polygon = new RegularPolygon(1, 50);
    }

    [Test]
    public void Constructor()
    {
        Assert.That(_polygon.VertexCount,Is.GreaterThanOrEqualTo(3));
    }

}

The above test also passes and I cannot figure out why!

At first I thought ReSharper might have been messing something up because it greys out the line and displays this message whenever I try to use a method in the Contract namespace:

Method invocation is skipped. Compiler will not generate method invocation because the method is conditional, or it is partial method without implementation.

But suspending R# and running the tests in NUnit has the same result with no errors or warnings in VS either. So I assume that is just because ReSharper does not have highlighting compatibility for code contracts yet.

I have looked at the documentation and as far as I can tell I shouldn’t be having this problem.

Am I using code contracts incorrectly or is my environment preventing it from working somehow?

Thank you.

  • 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-16T08:00:31+00:00Added an answer on May 16, 2026 at 8:00 am

    First thing to check – have you actually got contract checking turned on? If not, none of your contracts will do anything. That would explain the R# warning, too. Look under “Code Contracts” in the build properties, and see what it says under “Runtime Checking”.

    As per comments, ensure you have CONTRACTS_FULL defined as a compilation symbol – that appears to be what R# requires.

    Second point: you’ve got public mutable fields, which means your invariant can be violated at any moment by someone writing

    polygon.VertexCount = 0;
    

    Please don’t use public fields, particularly not writable ones. 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 493k
  • Answers 493k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The former, since it includes a reference to your company. May 16, 2026 at 10:42 am
  • Editorial Team
    Editorial Team added an answer It doesn't work like that. getEval() returns a string representation… May 16, 2026 at 10:42 am
  • Editorial Team
    Editorial Team added an answer You can modify the linker flags in MSC using #pragma… May 16, 2026 at 10:42 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

See this code: public class multiply { public Thread myThread; public int Counter {
In this code : public class MyClass { private Object innerValue; public Object getInnerValue()
Given this code: public class Messager implements Runnable { public static void main(String[] args)
Consider this code: public class ShortDivision { public static void main(String[] args) { short
I have this code: public class Home extends Activity{ @Override public void onCreate(Bundle savedInstanceState)
Consider this code: public class Main { static String s = -; public static
using this code: public class DvdRomUtility { [DllImport(winmm.dll, EntryPoint = mciSendStringA, CharSet = CharSet.Ansi)]
I wrote this code: public class FileViewer extends JPanel implements ActionListener { /** *
I have a this code public class SomeClass<T>: IEnumerable<T> { public List<SomeClass<T>> MyList =
I have this code: public class Window extends JFrame { public Window(){ ... JButton

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.