CodeContracts: Possibly calling a method on a null reference

Posted by dtb on Stack Overflow See other posts from Stack Overflow or by dtb
Published on 2010-04-20T23:27:02Z Indexed on 2010/04/20 23:33 UTC
Read the original article Hit count: 136

I'm having an argument with the CodeContracts static analysis tool.

My code:

class Foo
{
    private static Foo instance = new Foo();

    private string bar;

    public Foo()
    {
        bar = "Hello World";
    }

    public static int BarLength()
    {
        return instance.bar.Length;  //  <<--
    }
}

The tool tells me that instance.bar may be a null reference. I believe the opposite.

Who is right? How can I prove it wrong?

© Stack Overflow or respective owner

Related posts about code-contracts

Related posts about visual-studio-2010