1

JetBrains annotations:

Indicates that a method does not make any observable state changes. The same as System.Diagnostics.Contracts.PureAttribute

Microsoft Code Contracts:

Indicates that a type or method is pure, that is, it does not make any visible state changes.

When operating with primitive types it is very easy to know if your method is a pure function or not.

Consider this scenario however:

public class Program
{
    public static void Main()
    {
        var dodgy = new DodgyClass();

        string.Format("{0}", dodgy);
    }
}

public class DodgyClass
{
    public int State = 0;

    public override string ToString()
    {
        State = new Random().Next();
        return State.ToString();
    }
}

ReSharper tells me that "return value of pure method is not used" which is obviously incorrect since the state does change and in a very visible way. Unless of course they mean that any static method is always pure? Otherwise none of LINQ extension methods are pure since I can do all sort of state manipulation when overriding GetEnumerator().

Den
  • 1,827
  • 3
  • 25
  • 46
  • I think it's indicating that `string.Format` *should* be pure... which would be the case if your `ToString` method were pure, which is assumed to be the case by default, I believe. – Jon Skeet Jun 02 '14 at 15:01
  • The language both of them are using in their documentation is stronger then "should" and that confuses me. – Den Jun 02 '14 at 15:02
  • 1
    Sure. It's a matter of one contract relying on another, effectively :( – Jon Skeet Jun 02 '14 at 15:03
  • @JonSkeet do you want to post it as an answer? – Den Jun 04 '14 at 08:34

0 Answers0