简体   繁体   English

什么是c#/ VB.net的Benifit / Advantage优势属性 - Setters的私有访问修饰符

[英]What are the Benifit/Advantage of c#/VB.net Property Properties - Private access modifiers for Setters

Below is the class with a property. 以下是带有财产的类。

public class abc
    {
        public int MyProperty { get; private set; }
    }

Confusion - What's the benefit of typing private access modifier in setter ? 混淆 - 在setter中输入私有访问修饰符有什么好处?

Simply, it's a property that the class itself is allowed to set, but external objects can only read. 简单来说,它是允许类本身设置的属性,但外部对象只能读取。 Perhaps MyProperty changes as a side effect to a method, perhaps it is only set once (in a constructor). 也许MyProperty作为方法的副作用而改变,也许它只设置一次(在构造函数中)。 The main point is the source of change with MyProperty has to come from within abc (or a nested class of abc ), not from something outside that holds a reference to it. 主要的一点是与变化的源MyProperty具有来自内部abc (或嵌套类的abc ),而不是从外部的东西保持它的一个引用。

As for why you might use it, perhaps outside code cannot be trusted to set this value. 至于为什么你可能会使用它,也许不能信任外部代码来设置这个值。 The class isn't strictly immutable , it can change, but the only code trusted to do it exists inside the class (or a nested class). 该类不是严格不可变的 ,它可以改变,但是唯一可靠的代码存在于类(或嵌套类)中。 The outside world can simply read. 外面的世界可以简单地阅读。

private修饰符允许属性在公共,受保护或内部访问的上下文中是只读的,同时赋予类型本身设置属性的能力(即,在私有访问的上下文中)。

There are a couple reasons to use private set. 使用私人套装有几个原因。

1) If you are not using a backing field at all and want a read-only automatic property: 1)如果您根本没有使用支持字段并且想要只读自动属性:

public class abc
    {
        public int MyProperty { get; private set; }
    }

2) If you want to do extra work when you modify the variable inside your class and want to capture that in a single location: 2)如果您想修改类中的变量并希望在单个位置捕获该变量,则需要执行额外的工作:

private string _name = string.Empty;
public string Name 
{ 
    get { return _name; }
    private set 
    {
        TextInfo txtInfo = Thread.CurrentThread.CurrentCulture.TextInfo;
        _name = txtInfo.ToTitleCase(value);
    }
}

In general, though, it's a matter of personal preference. 但总的来说,这是个人偏好的问题。 Far as I know, there are no performance reasons to use one over the other. 据我所知,没有性能原因可以使用一个而不是另一个。

This is done to make your property read-only so that the external world is not allowed to change the value of the property and only the class implementing the property can change the property value being the owner of the property. 这样做是为了使您的属性为只读,以便不允许外部世界更改属性的值,只有实现该属性的类才能更改属性值作为属性的所有者。 As an example of how a class tracks its instance count and the instance count only can be increased/decreased from inside the class and the external world should not be allowed to change the instance count property eg: 作为类如何跟踪其实例计数和实例计数的示例,只能从类内部增加/减少,并且不应允许外部世界更改实例计数属性,例如:

public class Customer
{    
    public Customer()
    {
        InstanceCount++;
    }

    //Helps retrieving the total number of Customers
    public int InstanceCount { get; private set; } //Count should not be increased by the clients of this class rather should be increased in the constructor only
}

Another benefit in some situations is, after giving a private set to your property you can give a Set method for changing the property value from external world when you want to do some calculations or validations on the value received (which is not a best practice to do inside the Set property accessors), and then change the value of the property as follows: 在某些情况下的另一个好处是,在为您的属性提供私有设置后,您可以提供一个Set方法,以便在您希望对收到的值进行某些计算或验证时更改外部世界的属性值(这不是最佳实践)在Set属性访问器中执行,然后更改属性的值,如下所示:

public class Customer
{
    public string City { get; private set; }

    public bool SetCity(string customerCity)
    {
        //validate that the customerCity is a valid USA city or else throw some business rule exception, and then call below code
        City = customerCity
    }
}

私有setter允许只在内部设置属性,而getter仍公开公开属性值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM