C# 如何使用通用 getter 和 setter 设置只读属性的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/565075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:23:07  来源:igfitidea点击:

How to set the value of a read-only property with generic getters and setters?

c#properties

提问by mattruma

Not sure if I worded this correctly ... but I have the following code:

不确定我的措辞是否正确......但我有以下代码:

    public Guid ItemId
    {
        get;
    }

    public TransactionItem()
    {
        this.ItemId = Guid.Empty;
    }

Naturally I am getting a read-only issue ... which I do understand. Is there anyway to set this property value without having to do something like the below:

当然,我遇到了一个只读问题……我确实理解。无论如何设置此属性值而不必执行以下操作:

    Guid _itemId = Guid.Empty;
    public Guid ItemId
    {
        get
        {
            return _itemId;
        }
        set
        {
            _itemId = value;
        }
    }

or

或者

    public Guid ItemId
    {
        get;
        internal set;
    }

Thanks in advance!

提前致谢!

采纳答案by Svish

I would go for this:

我会这样做:

public Guid ItemId
{
    get;
    private set; // can omit as of C# 6 
}

public TransactionItem()
{
    this.ItemId = Guid.Empty;
}

Of course, it would be open for setting within this class, but since you are writing it I hope you have the sense to not break your own intentions...

当然,它会在这个类中开放设置,但既然你正在写它,我希望你有意识不要破坏自己的意图......

In my opinion, things like readonly properties, are mostly important when seen from the outside. From the inside, it doesn't really matter what it is, cause there, you are the King =)

在我看来,像 readonly 属性这样的东西从外面看是最重要的。从内部看,它是什么并不重要,因为在那里,你是国王 =)

回答by Jakub ?turc

You can use readonlykeyword

您可以使用readonly关键字

public readonly Guid ItemId;

public TransactionItem()
{
    this.ItemId = Guid.Empty;
}

回答by Jon Skeet

I'm afraid your question isn't very clear - but if there isn't a property setter defined, then you certainly can't call it.

恐怕您的问题不是很清楚-但是如果没有定义属性设置器,那么您当然不能调用它。

Are you actuallyafter an automatically implemented read-only property, allowing setting just within the constructor? If so, I'm afraid that's not available, much as I'd like it.

实际上是在自动实现只读属性之后,允许仅在构造函数中进行设置吗?如果是这样,恐怕这不可用,就像我希望的那样。

Just to expand on what I mean, I'd liketo be able to do:

只是为了扩大我的意思,我希望能够做到:

// Not valid in C# - yet!
public class Foo
{
    // Autogenerated field would be readonly in IL.
    public string Name { get; readonly set; }

    public Foo (string name)
    {
        this.Name = name;
    }

    public void Bar()
    {
        // This would be invalid
        this.Name = "No!";
    }
}

Basically it would be "make a property like a readonly field."

基本上它会“使属性像只读字段一样”。

回答by Frederik Gheysels

Use a private setter, or set the backing field ?
(But, then you must make sure that the name of your backing field can be determined based on the property-name.
For instance by making sure that your backing field always has the same name as the property-name, but is prefixed with an underscore; like NHibernatedoes it using its access-strategies).

使用私有二传手,或设置支持字段?
(但是,您必须确保您的支持字段的名称可以根据属性名称确定。
例如,通过确保您的支持字段始终与属性名称具有相同的名称,但前缀为下划线;就像NHibernate使用它的访问策略一样)。

回答by LukeH

If you just need the ItemIdproperty to be read-only for external users of your class, then Svish's answer is the way to go.

如果您只需要该ItemId属性对您班级的外部用户是只读的,那么 Svish 的答案就是要走的路。

If you need ItemIdto be read-only within the class itself then you'll need to do something like this:

如果您需要ItemId在类本身中只读,那么您需要执行以下操作:

private readonly Guid _ItemId
public Guid ItemId
{
    get { return _ItemId; }
}

public TransactionItem()
{
    _ItemId = Guid.Empty;
}