[英]Shorthand for Getters and Setters?
There's so many ways to write getters and setters that it confuses me.有很多方法可以编写 getter 和 setter,这让我感到困惑。 Are this two doing the same thing?
这两个在做同样的事情吗?
private List<MyClass> myPrivateList;
//Method 1
public List<MyClass> MyPublicList{ get => myPrivateList; private set { } }
//Method 2
public List<MyClass> MyPublicList=> myPrivateList;
And if so, does this allows me to see this propierty from another class while not allowing me to edit it?如果是这样,这是否允许我从另一个 class 看到这个属性,同时不允许我编辑它?
Thank you:).谢谢:)。
The getters in method 1 and method 2 are equivalent.方法 1 和方法 2 中的 getter 是等价的。 Method 1 also exposes a setter that does nothing, which is not quite the same as method 2. That would lead to confusion, because from within the declaring class it lets you write things like
方法 1 还公开了一个什么都不做的 setter,这与方法 2 不太一样。这会导致混淆,因为在声明 class 中,它可以让您编写类似的东西
this.MyPublicList = new List<MyClass>();
While that line looks like it should do something, the body of the setter is empty, so it doesn't do anything.虽然那行看起来应该做点什么,但 setter 的主体是空的,所以它什么也不做。 I think what you're going for would be to just not specify the setter at all:
我认为您要做的就是根本不指定设置器:
public List<MyClass> MyPublicList { get => myPrivateList; }
If you do want the setter to be usable privately, then you'd need to define the body:如果您确实希望 setter 可以私下使用,那么您需要定义主体:
public List<MyClass> MyPublicList { get => myPrivateList; private set => myPrivateList = value; }
Either way, someone will be able to edit the list, as when anyone gets myListReference = instance.MyPublicList;
无论哪种方式,有人都可以编辑列表,就像任何人得到
myListReference = instance.MyPublicList;
, they get a reference to myPrivateList
. ,他们获得对
myPrivateList
的引用。
If you want to get a shallow copy of myPrivateList
, you would want to do get=>new List<MyClass>(myPrivateList);
如果您想获得
myPrivateList
的浅表副本,则需要执行get=>new List<MyClass>(myPrivateList);
. . That would allow the removal or addition of items in
myListReference
without affecting myPrivateList
.这将允许删除或添加
myListReference
中的项目而不影响myPrivateList
。
But even then, that is merely a shallow copy, so if the items of myListReference
are edited, the same items will be edited in myPrivateList
.但即便如此,这只是一个浅拷贝,所以如果
myListReference
的项目被编辑,相同的项目将在myPrivateList
中被编辑。 To prevent that, you would need to do a deep copy.为了防止这种情况,您需要进行深层复制。
Exactly how to go about that deep copy would depend on the exact nature of MyClass
but you might learn more information from this question , using a class that defines a copy constructor .究竟如何 go 将取决于
MyClass
的确切性质,但您可能会从这个问题中了解更多信息,使用定义复制构造函数的 class。
private MyType _member;
public MyType GetMember() { return _member; }
public void SetMember(MyType value) { _member = value };
This is the basic way to protect a private member with public getter and setter methods (a property).这是使用公共 getter 和 setter 方法(属性)保护私有成员的基本方法。 Regardless of how you define a property, this is the C# equivalent of what will be created in MSIL .
无论您如何定义属性,这都是 C# 等效于将在MSIL中创建的属性。 That's also why you will have the
value
keyword available in the setter.这也是为什么您将在 setter 中使用
value
关键字的原因。 Every other method is merely syntactic sugar.其他所有方法都只是语法糖。
The Options are:选项是:
// Option 1
public MyType Member { get; set; }
// Option 2
private MyType _member;
public MyType Member
{
get
{
return _member;
}
set
{
_member = value;
}
}
// Option 3
public MyType Member
{
get => _member;
set => _member = value;
}
// Option 4
public MyType Member => _member; //Only a getter, yet even shorter.
What you can do, is not define a setter, this means you can't do an assignment with the property outside like Member = new MyType()
.您可以做的不是定义 setter,这意味着您不能对外部属性进行分配,例如
Member = new MyType()
。 However, you are still able to access any methods from the outside, that in turn change the value of the underlying data-structure like in Member.Clear()
.但是,您仍然可以从外部访问任何方法,从而改变底层数据结构的值,例如
Member.Clear()
。 Like @Ruzihm pointed out in his excellent answer, you would have to do object-Copying to provide an "uninteractive" copy, that provides "full" protection of the original.就像@Ruzihm 在他的出色回答中指出的那样,您必须进行对象复制以提供“非交互式”副本,从而提供对原件的“完全”保护。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.