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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:29:03+00:00 2026-05-16T15:29:03+00:00

When compiling code which uses code contracts, I have a very strange error I

  • 0

When compiling code which uses code contracts, I have a very strange error I don’t understand.

[ContractInvariantMethod]
private void ObjectInvariant()
{
    Contract.Invariant(
        this.isSubsidiary ||
        this.parentCompanyId == default(Guid));
}

fails with the following error:

Malformed contract. Found Invariant after assignment in method ‘<ProjectName>.ObjectInvariant’.

If the code is modified like this:

[ContractInvariantMethod]
private void ObjectInvariant()
{
    Contract.Invariant(
        this.isSubsidiary ||
        this.parentCompanyId == Guid.Empty);
        // Noticed the Guid.Empty instead of default(Guid)?
}

it compiles well.

What’s wrong with my default(Guid)?

  • 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-16T15:29:04+00:00Added an answer on May 16, 2026 at 3:29 pm

    The IL generated for this:

    Console.WriteLine("{0}, {1}", default(Guid), Guid.Empty);
    

    is:

        .locals init (
            [0] valuetype [mscorlib]System.Guid CS$0$0000)
        L_0000: nop 
        L_0001: ldstr "{0}, {1}"
        L_0006: ldloca.s CS$0$0000
        L_0008: initobj [mscorlib]System.Guid
        L_000e: ldloc.0 
        L_000f: box [mscorlib]System.Guid
        L_0014: ldsfld valuetype [mscorlib]System.Guid [mscorlib]System.Guid::Empty
        L_0019: box [mscorlib]System.Guid
        L_001e: call void [mscorlib]System.Console::WriteLine(string, object, object)
    

    Which corresponds to something like:

    Guid CS$0$0000 = new Guid();
    Console.WriteLine("{0}, {1}", CS$0$0000, Guid.Empty);
    

    Code Contracts works directly on the IL, so it thinks you’ve written something like the second version. The rewriter is saying you’re not allowed to assign to variables before the contracts, so it gives an error.

    However, this is weird, because while this doesn’t work:

    var x = new Guid();
    Contract.Invariant(
        this.isSubsidiary ||
        this.parentCompanyId == x);
    

    this does, but it is clearly an “assignment before Invariant”!

    var x = Guid.Empty;
    Contract.Invariant(
        this.isSubsidiary ||
        this.parentCompanyId == x);
    

    I think they actually modified the checker to allow some assignments like this (for ease of use), but that they haven’t allowed all cases… whether this is intended or not is beyond my knowledge.

    I’d report this on the Code Contracts forum, it may be a bug.

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

Sidebar

Ask A Question

Stats

  • Questions 543k
  • Answers 543k
  • 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 Pipes and Named pipes are good solution to communicate between… May 17, 2026 at 6:16 am
  • Editorial Team
    Editorial Team added an answer typedef enum works exactly the same in Objective-C as it… May 17, 2026 at 6:16 am
  • Editorial Team
    Editorial Team added an answer Put something like this code in your control: if(DesignMode) return;… May 17, 2026 at 6:16 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

I've discovered (below) that I need to use -fblocks when compiling code which uses
I'm compiling some code which uses libcurl on a Debian Linux system. My dev
I have some older AS2-style Haxe code which uses flash.Lib.Current.CreateEmptyMovieClip() to create a slideshow
When compiling the following code: void DoSomething(int Numbers[]) { int SomeArray[] = Numbers; }
I have static library and another program which uses it. In the static library
I have some C# code which is using CSharpCodeProvider.CompileAssemblyFromSource to create an assembly in
I have a project which uses C++/CLI to implement a GUI and some background
I am dynamically compiling code in my client application. When I start the application
Take this non-compiling code for instance: public string GetPath(string basefolder, string[] extraFolders) { string
I am compiling a legacy C code here and there is a lot of

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.