本文介绍了经过裁判?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然感到困惑路过的参考。

I am still confused about passing by ref.

如果我有,我想访问/提供给多个对象的缓存对象,我注入它使用构造器注入。我想影响我创建了一个缓存对象。如:

If I have a Cache object which I want to be accessed/available to a number of objects, and I inject it using constructor injection. I want it to affect the single cache object I have created. eg.

public class Cache {

   public void Remove(string fileToRemove) {
      ...
   }
}

public class ObjectLoader {

   private Cache _Cache;

   public ObjectLoader(Cache cache) {

   }

   public RemoveFromCacheFIleThatHasBeenDeletedOrSimilarOperation(string filename) {
      _Cache.Remove(fileName);
   }
}

我应该使用裁判时,我通过缓存到ObjectLoader构造?

Should I be using ref when I pass the Cache into the ObjectLoader constructor?

推荐答案

没有你不需要使用ref关键字在这种情况下。

No you do not need to use the ref keyword in this situation.

Cache是​​一个类,它是一个引用类型。当参考通入的方法,该参考(而不是对象本身)的一个副本放置到参数。两篇参考文献的方法的,内,外,都指向在堆上同一个对象,并使用一个对象的字段的修改将被反映在其它

Cache is a class, it is a reference type. When a reference is passed into a method, a copy of the reference (not the object itself) is placed into your parameter. Both references, inside and outside, of the method are pointing to the same object on the heap, and modification of the object's fields using one will be reflected in the other.

添加裁判的方法调用原始引用传递。这是一个情况下,你会被重新分配(即通过调用)的位置的参考点,从内调用方法。

Adding ref to your method call passes in the original reference. This is useful in a situation where you would be reassigning (ie. by calling new) the location the reference points to from within a calling method.

这篇关于经过裁判?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 15:37