不纯的方法被调用为只读字段

不纯的方法被调用为只读字段

本文介绍了不纯的方法被调用为只读字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的的Visual Studio 2010 + ReSharper的的,它显示在下面的代码警告:

I'm using Visual Studio 2010 + Resharper and it shows a warning on the following code:

if (rect.Contains(point))
{
    ...
}

RECT 只读矩形字段,ReSharper的显示我这样的警告:

rect is a readonly Rectangle field, and Resharper shows me this warning:

。不纯的方法被调用值类型的只读域

什么是不纯的方法,为什么被证明我这个警告呢?

推荐答案

首先,乔恩,迈克尔和Jared的答案基本上是正确的,但我有一些事情,我想加入到他们。

First off, Jon, Michael and Jared's answers are essentially correct but I have a few more things I'd like to add to them.

什么是一个不纯的方法?

这是比较容易表征纯的方法。 纯的方法有以下特点:

It is easier to characterize pure methods. A "pure" method has the following characteristics:


  • 它的输出完全是由它的输入确定;它的输出不依赖于像一天的时间,或在硬盘位的外部性。它的输出不依赖于它的历史;调用带有给定参数的方法两次应该给予同样的结果。

  • 一个纯粹的方法,它周围的世界产生任何可观察到的突变。一个纯粹的方法可以选择变异私有状态效率的缘故,而是一个纯粹的方法不对,比如说,发生变异的参数的领域。

例如, Math.Cos 是一个纯粹的方法。它的输出仅取决于它的输入,并且所述输入没有被呼叫改变

For example, Math.Cos is a pure method. Its output depends only on its input, and the input is not changed by the call.

这是不纯的方法是它不是纯的方法。

An impure method is a method which is not pure.

什么是一些路过只读结构不纯方法的危险性?

有两种浮现在脑海中。首先是由Jon,迈克尔和Jared一指出,这是ReSharper的警告你有关的人。当你调用一个结构的方法,我们总是传递给是接收机的变量的引用,以防方法想变异变量。

There are two that come to mind. The first is the one pointed out by Jon, Michael and Jared, and this is the one that Resharper is warning you about. When you call a method on a struct, we always pass a reference to the variable that is the receiver, in case the method wishes to mutate the variable.

那么,如果调用上的值这样的方法,而不是一个变量?在这种情况下,我们做一个临时变量,值复制到它,并传递给变量的引用

So what if you call such a method on a value, rather than a variable? In that case we make a temporary variable, copy the value into it, and pass a reference to the variable.

一个只读变量被认为是一个值,因为它不能突变的构造之外。因此,我们要复制变量到另一个变量,不纯的方法可能是变异副本,当你想让它发生变异的变量。

A readonly variable is considered a value, because it cannot be mutated outside the constructor. So we are copying the variable to another variable, and the impure method is possibly mutating the copy, when you intend it to mutate the variable.

这是传递一个危险只读结构作为的接收的。也有通过包含只读字段一个结构的危险。包含只读字段的结构是一种常见的做法,但它本质上是写检查该类型系统没有足够的资金,以现金;该特定变量的只读性由存储的所有者确定。引用类型的实例拥有自己的存储,但值类型的实例不会!

That's the danger of passing a readonly struct as a receiver. There is also a danger of passing a struct that contains a readonly field. A struct that contains a readonly field is a common practice, but it is essentially writing a cheque that the type system does not have the funds to cash; the "read-only-ness" of a particular variable is determined by the owner of the storage. An instance of a reference type "owns" its own storage, but an instance of a value type does not!

struct S
{
  private readonly int x;
  public S(int x) { this.x = x; }
  public void Badness(ref S s)
  {
    Console.WriteLine(this.x);
    s = new S(this.x + 1);
    // This should be the same, right?
    Console.WriteLine(this.x);
  }
}



人认为这一点。 X 是不会改变的,因为x是一个只读域和不是一个构造函数。但是......

One thinks that this.x is not going to change because x is a readonly field and Badness is not a constructor. But...

S s = new S(1);
s.Badness(ref s);



...清楚地表明那是虚假的。 这个取值指的是同一个变量,而的变量不是只读!

... clearly demonstrates the falsity of that. this and s refer to the same variable, and that variable is not readonly!

这篇关于不纯的方法被调用为只读字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 22:09