Can you create objects through the Property setter?

C #: can you create objects through a set of properties?

eg.

private List<MyObject> myList;
public List<MyObject> MyListProperty { get {return myList;} set {myList = value;} }

THEN

MyListProperty = new List<MyObject>();
+3
source share
1 answer

Yes, that’s absolutely true.
In a line, MyListProperty = new List<MyObject>();you do not create objects through the property setting tool. First you create a new list, and then you set MyListPropertyto the list that you created. This is equivalent to:

List<MyObject> myObjectList = new List<MyObject>();
MyListProperty = myObjectList;

Next, if you want your code to compile, you must specify the type of your property:

public List<MyObject> MyListProperty
{
     get {return myList;}
     set {myList = value;}
}
+5
source

Source: https://habr.com/ru/post/1759262/


All Articles