如何从自动实现的属性中的字段访问dispose方法

时间:2013-08-23 14:14:59

标签: c# .net memory .net-4.0

为什么我可以访问Bmw getter setter方法中的dispose方法而不是dispose()方法?

我如何处理以下字段:?

Class Car: IDisposable
 {

    private FontWeight bmw;

     public FontWeight Bmw
                {
                   bmw.Dispose(); <<<<<<<< Can access Dispose
                    get
                    { return bmw; }
                    set
                    { bmw= value;

                    }

                    public void Dispose(){
                      bmw.Dispose(); <<<< Cant access Dispose()
                    }                        
                }
            }

1 个答案:

答案 0 :(得分:2)

如果这是您的代码,那么您内部会遇到大量语法错误:

public FontWeight Bmw
{
    /// here shouldn't be any code, just getters and setters
    get { return bmw; }
    set { bmw = value; }

    /// you forgot to close the property here
} /// now it's closed

public void Dispose()
{
    bmw.Dispose(); /// now it will work
}