开发者

Question about [Pure] methods

开发者 https://www.devze.com 2022-12-29 05:39 出处:网络
Is the following method Pure? I\'d say so, as it doesn\'t change in anyway the current class, thus, everything we can now currenly \"see开发者_如何转开发\" in the class, before running this method wil

Is the following method Pure? I'd say so, as it doesn't change in anyway the current class, thus, everything we can now currenly "see开发者_如何转开发" in the class, before running this method will still be exactly the same after. Am I correct?

class Set {
    ...
    public ISet<T> UnionWith(ISet<T> set) {
       ISet<T> unionSet = ...

        foreach (Element element in this) {
            unionSet.Add(element);
        }

        foreach (Element element in set) {
           unionSet.Add(element);
        }

        return unionSet;
    }
}


If by [Pure] you mean labeled with the Pure attribute from System.Diagnostics.Contracts, the documentation says:

Pure methods do not make any visible state changes.

Since your method appears to not make any visible state changes (i.e. no side effects), it would qualify for the [Pure] attribute.

0

精彩评论

暂无评论...
验证码 换一张
取 消