DevLabs > DevLabs Forums > Code Contracts > Framework 'Contains' method is declared as Impure
Ask a questionAsk a question
 

AnswerFramework 'Contains' method is declared as Impure

  • Sunday, November 01, 2009 6:05 PMDavid K Allen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I would think that 'System.Collections.Generic.Stack`1<System.String>.Contains(System.String)' would be free of side-effects and thus eligible to be declared Pure. The error below warns me about this. Is it appropriate to add the [Pure] attribute to the 'Contains' method of Generic.Stack?
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    
    namespace SampleContracts.Test
    {
        public class Foo
        {
            public Stack<string> Deletables = new Stack<string>();
            public void Delete(string item)
            {
                Contract.Requires(item != null);
                /* Code Contracts compiler issues this warning
                     * Warning	4	
                     * Detected call to impure method 
                     * 'System.Collections.Generic.Stack`1<System.String>.Contains(System.String)' 
                     * in a pure region in method 
                     * 'SampleContracts.Test.Foo.Delete(System.String)'.	
                     * ...
                     */
                Contract.Requires(Deletables.Contains(item));
            }
        }
    }
    


    Best Regards, David K Allen Minneapolis, Minnesota, USA

Answers

All Replies