Contract attributes on fields should be checked in constructor

michael.proepster's Avatar


15 Feb, 2018 09:00 AM

internal class CodeContractTest

    private string _someMember;

    public string SomeMember
        get { return _someMember; }
        set { _someMember = value; }

_someMember can still be null if I forget to initialize it in the constructor. This violates the contract.
There should be a check at the end of the constructor.

  1. Support Staff 1 Posted by PostSharp Techn... on 19 Feb, 2018 03:44 PM

    PostSharp Technologies's Avatar


    The current behavior of the code contract aspects is according to the original design. We are aware that the validation after constructor completion is missing. This feature is currently in the backlog and will be implemented in one of the future versions. You can also add a request on our user voice website ( to raise the priority of the feature.


  2. Support Staff 2 Posted by PostSharp Techn... on 28 Feb, 2018 09:06 AM

    PostSharp Technologies's Avatar


    We are going to close this request as we believe it was answered. Please feel free to reopen the discussion if you need more help.

    Best regards,
    PostSharp Team

  3. PostSharp Technologies closed this discussion on 28 Feb, 2018 09:06 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac