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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…