Search code examples
c#-4.0.net-4.0static-analysiscode-contracts

What is the most effective way in .NET 4.0 to require input parameter collection to be sorted using Code Contracts?


How to implement input parameter sorted precondition enforcement?


Solution

  • A bit of code would help us all with helping you.

    I'll assume you have a method along the lines of this..

    public void MyMethod(params string[] list) {...}
    

    and you want to use Code Contracts to ensure that this method will only ever be called with a sorted list. Did you try something like the following for the body?

    Contract.Requires(list.OrderBy(s => s).SequenceEquals(list));