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 6923509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:32:40+00:00 2026-05-27T10:32:40+00:00

I have several questions regarding code contracts, and the best practices for their usage.

  • 0

I have several questions regarding code contracts, and the best practices for their usage. Lets say we have a class, with several properties (see below for example):

class Class1
{
    // Fields
    private string _property1;       //Required for usage
    private List<object> _property2; //Not required for usage

    // Properties
    public string Property1 
    { 
        get
        {
            return this._property1;
        }            
        set
        {
            Contract.Requires(value != null);
            this._property1 = value;
        } 
    }
    public List<object> Property2 
    { 
        get
        {
            return this._property2;
        }
        set
        {
            Contract.Requires(value != null);
            this._property2 = value;
        }
    }

    public Class1(string property1, List<object> property2)
    {
        Contract.Requires(property1 != null);
        Contract.Requires(property2 != null);

        this.Property1 = property1;
        this.Property2 = property2;
    }

    public Class1(string property1)
        : this(property1, new List<object>())
    { }
}

Some explanation about what I want to achieve:

(a) property1 is a required field. property2 is not explicitly required for normal usage of the object.

I have the following questions:

  1. Should I even bother with the contracts for property2; because property2 is not a required field, should it have a contract at all. Does placing a contract on property2 indicate that it is in fact required for normal usage of the object;

  2. Even though property2 is not explicitly required, there is no possible reason for it to be null, thus the defined contract at the setter. Wouldn’t defining the contract on property2 reduce the null checks in calling code? This should reduce bugs and improve maintainability of the code – is this assumption correct?

  3. If it is right, how do I ensure to calling code that property2 will never be null? Do I use Contract.Invariant(property2 != null); or Contract.Ensures(property2 != null) in the constructor, or Contract.Ensures(property2 != null) in the Init(), or Contract.Ensures(property != null) in the setter? (i.e. if using Contract.Ensures(property2 != null), where is it placed)?

My apologies if the questions seem simple. I am just looking for thoughts on the matter, and what you folks consider best practice.

  • 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-27T10:32:41+00:00Added an answer on May 27, 2026 at 10:32 am

    This is what I would recommend as far as Contracts:

        class Class1
        {
            // Fields
            private string _property1;       //Required for usage
            private List<object> _property2; //Not required for usage
    
            // Properties
            public string Property1
            {
                get
                {
                    Contract.Ensures(Contract.Result<string>() != null);
                    return this._property1;
                }
                set
                {
                    Contract.Requires(value != null);
                    this._property1 = value;
                }
            }
    
            public List<object> Property2
            {
                get
                {
                    Contract.Ensures(Contract.Result<List<object>>() != null);
                    return this._property2;
                }
                set
                {
                    Contract.Requires(value != null);
                    this._property2 = value;
                }
            }
    
            public Class1(string property1, List<object> property2)
            {
                Contract.Requires(property1 != null);
                Contract.Requires(property2 != null);
    
                this.Property1 = property1;
                this.Property2 = property2;
            }
    
            public Class1(string property1)
                : this(property1, new List<object>())
            {
                Contract.Requires(property1 != null);
            }
    
            [ContractInvariantMethod]
            private void ContractInvariants()
            {
                Contract.Invariant(_property1 != null);
                Contract.Invariant(_property2 != null);
            }
        }
    

    The properties have their public behavioral contracts and the Invariants will catch any errors you might introduce later as you add logic to Class1 that could modify the field values and thus violate the public contracts. Alternately, if the fields can be made readonly (and the setters removed), you don’t need the invariants.

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

Sidebar

Related Questions

I have several questions regarding filenames and the iPod Library. I understand I can
I have several questions regarding forms and PHP but if I should put them
I have several questions regarding where to handle nulls. Let me set up a
I know there have been several questions regarding the mousewheel and scrolling; I'm not
I have been through several SO questions regarding this and my approach is a
I have several questions regarding CI application design. Q. When creating a new form
There have been several questions on SO regarding getting Contacts numbers using the Contacts
I have a question regarding the best design pattern for code reuse when dealing
I have several questions: 1. What's the difference between isoMDS and cmdscale? 2. May
I have several questions concerning the controls like a button, if You could answer

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.