简体   繁体   中英

Shorthand for Getters and Setters?

There's so many ways to write getters and setters that it confuses me. 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?

Thank you:).

The getters in method 1 and method 2 are equivalent. 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

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. 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:

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; , they get a reference to myPrivateList .

If you want to get a shallow copy of myPrivateList , you would want to do get=>new List<MyClass>(myPrivateList); . That would allow the removal or addition of items in myListReference without affecting 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 . 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 .

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). Regardless of how you define a property, this is the C# equivalent of what will be created in MSIL . That's also why you will have the value keyword available in the setter. 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() . 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() . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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