gpt4 book ai didi

c# - PureAttribute 对参数的用途

转载 作者:可可西里 更新时间:2023-11-01 08:28:52 25 4
gpt4 key购买 nike

据我了解 PureAttribute用于标记某些东西(类、方法、委托(delegate)等)没有进行可见的更改,但我可以从以下定义中看出它可以应用于方法参数:

[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Constructor|AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Event|AttributeTargets.Parameter|AttributeTargets.Delegate, AllowMultiple = false, Inherited = true)]
public sealed class PureAttribute : Attribute

将此属性应用于参数的目的是什么,例如:

public void SomeMethod([Pure]SomeClass theParameter)
{
}

这是否意味着 SomeMethod 不应该在未标记为 [Pure]theParameter 上使用任何东西,这意味着我们可以确保实例SomeClass 的调用在调用 SomeMethod 前后是否明显相同?

我没有看到PureAttribute是这样用的,不知道是代码契约不支持还是我的理解有误?

最佳答案

如您所述,PureAttribute 表示类型或方法是纯的,也就是说,它不会进行任何可见的状态更改(直接取自 MSDN 文章)。

也许你的 SomeClass 没有被标记为纯的,因为它可以改变状态,但这并不意味着其中的一切都是不纯的。

也许你的 SomeMethod 没有使用任何 SomeClass 不纯的方法,也许它只是读取它的属性(我假设你没有在属性中执行不纯的操作 setter/getter ,否则你就是邪恶的),所以它对 SomeClass 的使用是纯粹的。

关于c# - PureAttribute 对参数的用途,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22198257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com