我正在看編寫一個類的例子,並使用構造函數,並想知道使用「this」的區別是什麼。或不。寫this.propertyName或只是propertyName之間的區別
那麼,什麼是之間的differnce:
public class PagedCountryList
{
public IEnumerable<Country> CountriesToDisplay { get; set; }
public int Total { get; set; }
public int PerPage { get; set; }
public int PageNumber { get; set; }
public PagedCountryList(IEnumerable<Country> countries, int totalResult, int elementsPerPage, int pageNumber)
{
this.CountriesToDisplay = countries;
this.Total = totalResult;
this.PerPage = elementsPerPage;
this.PageNumber = pageNumber;
}
}
這:
public class PagedCountryList
{
public IEnumerable<Country> CountriesToDisplay { get; set; }
public int Total { get; set; }
public int PerPage { get; set; }
public int PageNumber { get; set; }
public PagedCountryList(IEnumerable<Country> countries, int totalResult, int elementsPerPage, int pageNumber)
{
CountriesToDisplay = countries;
Total = totalResult;
PerPage = elementsPerPage;
PageNumber = pageNumber;
}
}
沒有差別。 '這個。'在這種情況下是多餘的 –
如果您的構造函數使用與您的屬性相同的參數,那麼將需要'this'。 –
感謝您的快速解答! – user3228992