c#Autoproperties和Dictionary

时间:2012-08-25 19:14:22

标签: c# dictionary automatic-properties

我有一个派生的基础案例,所以我有很少的int和字符串自动属性

// base class 

   public virtual int MyVariable {get; set; }; 


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

    }

我如何对词典做同样的事情?

1 个答案:

答案 0 :(得分:2)

它几乎相同,但您当然需要将类型从int更改为您的字典类型。

Dictionary<string,int>的示例:

基类:

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

派生类:

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

}