且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#属性:如何使用自定义设置属性没有私人领域?

更新时间:2023-10-14 15:32:52

在你想无论是在getter或无法使用自动属性了二传手的定制做任何事情。


I want to do this:

public Name
{
    get;
    set
    {
        dosomething();
        ??? = value
    }
}

Is it possible to use the auto-generated private field?
Or is it required that I implement it this way:

private string name;
public string Name
{
    get
    {
        return name;
    }
    set
    {
        dosomething();
        name = value
    }
}

Once you want to do anything custom in either the getter or the setter you cannot use auto properties anymore.