Question about [Pure] methods
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.
精彩评论