Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

how to store a reference variable in class c#

I am passing a classobject(Cache class object) as a reference in a constructor of 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
  }
}

Now in MyClass, I want to set the value of this cache

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"
    }
}

I want that changing the value of cache in Myclass should reflect in Test Class. But I am not sure how to achieve this. Any help would be appreciated.

Here is the complete scenario which I am trying to do. We have a no of multiple orgs and there are some properties of the org which are same for some org and we don't want to compute those property again and again as it is costly operation. That properties are computed in doSomethingMethod above.

My cache is actually a list. And this cache variable will be passed in the multiple orgs. So in dosomething method I want to check whether the cache is being set or not by any other org and if the key is present I will not compute operation and I will just return from cache.

question from:https://stackoverflow.com/questions/65915171/how-to-store-a-reference-variable-in-class-c-sharp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you omit the ref keyword, what you give to the constructor is a reference to the cache instance. The reference itself will be a copy, but still reference the same instance. If you then change properties of said instance, it will reflect through all other references to the same instance.

Consider this example:

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;
}

Output:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...