Search code examples
c#code-contracts

Saying the contract static checker that a extension method not allow null?


 Contract.Requires(completeURL.IsUri()); // Error: Contract.Requires(completeURL != null)

Can I do this without Contract.Assume()?
I mean something like this:

    [Pure]
    [DefinitelyNotANullStringAfterThatMethod]
    public static bool IsUri(this string str)
    {
        return Uri.IsWellFormedUriString(str, UriKind.Absolute);
    }

Solution

  •     [Pure]
        public static bool IsUri(this string str)
        {
            Contract.Ensures(!Contract.Result<bool>() || str != null); // by Dan Bryant
            return Uri.IsWellFormedUriString(str, UriKind.Absolute);
        }
    

    Maybe?