请告诉我如何在另一种方法中只调用此属性的get方法。 例如
public List<EmployeeData> LOP
{
get
{
if (_lop == null)
{
_lop = new List<DTPackage>();
}
return _lop;
}
set
{
_lop = value;
}
}
我想只调用此属性的get方法。
答案 0 :(得分:2)
public List<EmployeeData> LOP
{
get
{
if (_lop == null)
{
_lop = new List<DTPackage>();
}
return _lop;
}
set
{
_lop = value;
}
}
var lop = LOP; // here POP get will be called
LOP = myEmployeeList //here POP set will be called
您可以set
到private
以避免从其他类访问或删除set
以获取只读
答案 1 :(得分:0)
这些都是.Net属性的可编译变体:
// Shorthand
public string MyProperty1 { get; set; }
public string MyProperty2 { get; private set; }
public string MyProperty3 { get; }
// With backing field
private string _myProperty4;
private string _myProperty5;
private readonly string _myProperty6;
public string MyProperty4
{
get { return _myProperty4; }
set { _myProperty4 = value; }
}
public string MyProperty5
{
get { return _myProperty5; }
private set { _myProperty5 = value; }
}
public string MyProperty6
{
get { return _myProperty6; }
}
用法:
string myString = MyProperty4; // Calls get on MyProperty4 MyProperty4 = "Hello World" // Calls set on MyProperty4 MyProperty6 = "Hello World" // Will not be compilable