PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 5, 2022

[FIXED] how to store a reference variable in class c#

 July 05, 2022     c#, pass-by-reference     No comments   

Issue

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.


Solution

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


Answered By - Fildor
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing