4

The Substring method of C#'s string class (which is also available in every other .Net language) does not seem to alter state and will always return the same value, given the same arguments.

So is it a "pure" method?

I'm asking, because in Microsoft's reference source code of the System.String class, many other public methods are annotated as [Pure] (like Trim, Compare, IndexOf, Equals), but the Substring method is not.

  • I took the freedom to change the question to ask what you really want to know, and added the missing reference to Microsoft's source. Please double check if I got your intentions right. – Doc Brown Jul 27 at 7:44
  • 1
    I wonder if Substring calling InternalSubString, the latter of which marked unsafe due to pinning, would have explained why it cannot be marked as Pure despite meeting its practical requirements from a human understanding perspective. – rwong Jul 27 at 8:39
  • 1
    Note that unlike the general definition of pure functions, PureAttribute only mentions that it shouldn't change any visible state. – Vivelin Jul 27 at 8:50
  • @DocBrown, thanks, you got it right. My only note would be that I meant "every other programming language (like Java and etc)", as I suspect a Substring method is quite common, but I think it's OK. – user85128 Jul 27 at 9:20
  • 1
    @Vivelin, what does "shouldn't change any visible state" mean, exactly? Does it mean that the passed arguments (including the string on which the method is called) should not be changed, themselves? – user85128 Jul 27 at 9:21
6

Note that Microsoft uses [Pure] exclusively in the context of Code Contracts (as opposed to, say, optimizing code during compilation or runtime). In this context, if the method doesn't make visible state changes, the method is pure. As simple as that.

When Code Contracts were introduced, the [Pure] attribute was added sporadically and sometimes inconsistently, and a series of methods which are pure didn't have this attribute. Back then I was working on several critical projects which required us to use Code Contracts, and I had to do a lot of wrappers around .NET Framework's assemblies just to add proper contracts, for instance saying that Random.NextDouble will always return a value which is greater than or equal to 0.0 and less than 1.0. The same goes for [Pure].

Chances are, some developers at Microsoft needed to work with string.Trim within the code covered by Code Contracts, and so it got its attribute, but nobody used string.Substring. Or there may be other considerations why the later didn't receive its well deserved attribute.

Anyway, if you rely on Code Contracts, create a wrapper and add the missing attribute.

If you don't rely on Code Contracts, ignore those attributes.

| improve this answer | |
  • 1
    [Pure] has some other minor advantages outside Code Contracts - for example, some linters will pick up if the return value of a pure function isn't used. – Philip Kendall Jul 27 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.