C# 可以通过属性设置器实例化对象吗?

C# 可以通过属性设置器实例化对象吗?,c#,properties,C#,Properties,C#:能否通过属性设置器实例化对象 例如 私有列表myList; 公共列表MyListProperty{get{return myList;}set{myList=value;}} 然后: MyListProperty=new List(); 是的,这是完全正确的。 在MyListProperty=new List()行中不“通过属性设置器实例化对象”。首先,实例化一个新列表,然后将MyListProperty设置为已创建的列表。这相当于: List<MyObject> myObj

C#:能否通过属性设置器实例化对象

例如

私有列表myList;
公共列表MyListProperty{get{return myList;}set{myList=value;}}
然后:

MyListProperty=new List();

是的,这是完全正确的。
MyListProperty=new List()行中不“通过属性设置器实例化对象”。首先,实例化一个新列表,然后将
MyListProperty
设置为已创建的列表。这相当于:

List<MyObject> myObjectList = new List<MyObject>();
MyListProperty = myObjectList;
List myObjectList=new List();
MyListProperty=myObjectList;
接下来,如果要编译代码,应指定属性的类型:

public List<MyObject> MyListProperty
{
     get {return myList;}
     set {myList = value;}
}
公共列表MyListProperty
{
获取{return myList;}
设置{myList=value;}
}

您的属性
MyListProperty
缺少类型说明符。
List<MyObject> myObjectList = new List<MyObject>();
MyListProperty = myObjectList;
public List<MyObject> MyListProperty
{
     get {return myList;}
     set {myList = value;}
}