Search code examples
c#dictionaryautomatic-properties

c# Autoproperties and Dictionary


I have a base case that gets derived so i have few int and string Auto properties

// base class 

   public virtual int MyVariable {get; set; }; 


// Derived class
    private int myVariable = 0;
    public override int MyVariable 
    {
        get { return myVariable ; }
        set { myVariable = value; }

    }

How can i do the same with a Dictionary ?


Solution

  • It is pretty much the same, though you will of course need to change the type from int to your dictionary type.

    Example with a Dictionary<string,int>:

    Base class:

    public virtual Dictionary<string,int> MyVariable {get; set; }; 
    

    Derived class:

    private Dictionary<string,int> myVariable = new Dictionary<string,int>();
    public override Dictionary<string,int> MyVariable 
    {
        get { return myVariable ; }
        set { myVariable = value; }
    
    }