关于[纯]方法的问题
下面的方法是纯的吗?我想说的是,因为它无论如何都不会改变当前的类,因此,我们现在可以在类中“看到”的所有内容,在运行此方法之前仍然完全相同。我说得对吗?
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;
}
}
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
[Pure]
您的意思是标有Pure
属性来自 System.Diagnostics.Contracts,文档说:由于您的方法似乎没有进行任何可见的状态更改(即没有副作用),因此它符合
[Pure]
属性。If by
[Pure]
you mean labeled with thePure
attribute from System.Diagnostics.Contracts, the documentation says:Since your method appears to not make any visible state changes (i.e. no side effects), it would qualify for the
[Pure]
attribute.