【问题标题】:how to store a reference variable in class c#如何在类c#中存储引用变量
【发布时间】:2021-05-01 01:16:20
【问题描述】:

我在 Myclass 的构造函数中传递一个类对象(缓存类对象)作为引用。

public class Test{
  static void Main(string[] args){
     Cache<string, string> cache;
     Myclass obj = new MyClass(ref cache);
     obj.doSomething();
     Console.WriteLine(cache.key); // I want this value to get changed according to the doSomething Method
     Console.WriteLine(cache.value);// I want this value to get changed according to the doSomething Method
  }
}

现在在 MyClass 中,我想设置这个缓存的值

public class Myclass{
    ref Cache<string, string> C;
    Myclass(ref Cache<string, string> cache){
        this.C = cache;
    }
    void doSomething(){
        C.key = "key";
        C.value = "value"
    }
}

我希望更改 Myclass 中缓存的值应该反映在 Test Class 中。但我不确定如何实现这一目标。任何帮助将不胜感激。

这是我正在尝试做的完整场景。我们没有多个组织,并且该组织的某些属性对于某些组织是相同的,我们不想一次又一次地计算这些属性,因为它是昂贵的操作。这些属性是在上面的 doSomethingMethod 中计算出来的。

我的缓存实际上是一个列表。并且这个缓存变量将在多个 orgs 中传递。所以在 dosomething 方法中,我想检查缓存是否被任何其他组织设置,如果密钥存在,我将不计算操作,我只会从缓存中返回。

【问题讨论】:

  • ref 这里根本不需要。尽量不要使用它。
  • 什么是缓存?我认为你需要重新考虑设计。你的目标是什么?请尝试更好地解释您的问题、开发环境、数据类型和预期结果,并分享或多或少的代码(无屏幕截图)、屏幕图像或草图、用户故事或场景图,以及类似的示例是单元测试或 API 使用。正如我可以从提供的代码中推断出的那样,更改 Key 和 Value 应该在任何地方都体现出来......我不明白这里显示的这个设计。
  • 嗨@Damien_The_Unbeliever 感谢您的评论。这意味着如果我只是传递缓存对象(没有 ref 关键字),它会作为引用传递吗?
  • @DevendraVerma 有时描述目标可能有助于帮助以及理解编码问题,但要解决代码问题,Stack Overflow 的使命,我们需要代码。阅读几个像客户规范一样解释的句子,特别是因为我是法语而不是高级英语,这对我来说很困难,对于任何比拥有代码慢的人来说也是如此,甚至是错误的、坏的或伪代码而不是文学。所以请指出Cache 的最低定义,这不是一个列表,但似乎是一本字典,以及我之前评论中要求的样本。
  • 如果你省略了ref 关键字,你给ctor 的一个对cache 实例的引用。引用 itself 将是一个副本,但仍引用 same 实例。如果您随后更改所述实例的 properties,它反映到对同一 instance 的所有其他引用。

标签: c# pass-by-reference


【解决方案1】:

如果您省略了ref 关键字,那么您给构造函数的内容cache 实例的引用。引用本身将是一个副本,但仍引用相同的实例。如果您随后更改所述实例的属性,它将通过对同一实例的所有其他引用进行反映。

考虑这个例子:

using System;
                    
public class Program
{
    public static void Main()
    {
        MyCache sharedCache = new MyCache();
        Department dpt1 = new Department(sharedCache);
        Department dpt2 = new Department(sharedCache);
        
        Console.WriteLine($"Dpt1: {dpt1.CacheValue}");
        Console.WriteLine($"Dpt2: {dpt2.CacheValue}");
        
        sharedCache.Value = 1;
        Console.WriteLine($"Dpt1: {dpt1.CacheValue}");
        Console.WriteLine($"Dpt2: {dpt2.CacheValue}");
        
        dpt1.ChangeValue(2);
        Console.WriteLine($"Dpt1: {dpt1.CacheValue}");
        Console.WriteLine($"Dpt2: {dpt2.CacheValue}");
    }
}

public class Department
{
    private readonly MyCache cache;
    
    public Department(MyCache cache)
    {
        this.cache = cache;
    }
    
    public int CacheValue => this.cache.Value;
    public void ChangeValue(int newValue)
    {
        this.cache.Value = newValue;
    }
}

public class MyCache
{
    public int Value {get; set;} = default;
}

输出:

Dpt1: 0 Dpt2: 0 Dpt1:1 Dpt2:1 Dpt1:2 Dpt2:2

【讨论】:

    猜你喜欢
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 2019-04-17
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多