我目前有以下课程,包括getter和setter
public class CustAccount
{
public string Account { get; set; } private string account;
public string AccountType { get; set; } private string accountType;
public CustSTIOrder(Account ord)
{
account = ord.Account;
accountType = ord.AccountType;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我意识到,public string Account { get; set; }我不需要申报private string account.无论如何现在我的私有变量account包含值,但当我Account用来获取值时,我得到一个null.有关为什么我得到null的任何建议?
由于您使用的是自动属性,因此您应该使用Account该属性的所有引用.
如果你正在寻找使用支持字段,那么你就需要有支持字段(account在)get和set.
例:
public string Account
{
get { return account; }
set { account = value; }
}
private string account;
Run Code Online (Sandbox Code Playgroud)
自动属性的使用示例:
public CustSTIOrder(Account ord)
{
Account = ord.Account;
// the rest
}
Run Code Online (Sandbox Code Playgroud)