views:

98

answers:

1

I'm using Resharper 5.x to do compile-time analysis and it's usually pretty good about it, but it doesn't seem to be applying code contracts to its logic. I have something like the following, but I'm getting an issue on the marked line.

public void Method(int arg)
{
    Contract.Requires(this.NullableValueType != null);

    this.Method2(
        arg,
        this.NullableValueType.Value, // [1]
        this.ReferenceType);
}

[1] ends up getting highlighted with "Possible 'System.InvalidOperationException'". Is there a way to get rid of this error without turning off the check?

A: 

While admittedly Resharper could be more intelligent and take contracts into account, unfortunately currently it doesn’t.

I would recommend to make the line more explicit. Instead of

this.NullableValueType.Value

you could write

this.NullableValueType ?? <something>

where the “something” is, of course, something that doesn’t matter because it never happens (for example, new ThatValueType()).

Timwi