我正在看一个编写类并使用构造函数的示例,并想知道使用“ this”的区别是什么。或不。
那么,两者之间有什么区别?
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;
}
}
最佳答案
您的情况没有区别,但请考虑以下示例
public class PagedCountryList
{
private IEnumerable<Country> countries { get; set; }
public int totalResult { get; set; }
public int elementsPerPage { get; set; }
public int pageNumber { get; set; }
public PagedCountryList(IEnumerable<Country> countries, int totalResult, int elementsPerPage, int pageNumber)
{
//you cant write something like this (because it is ambiguous)
//countries = countries;
this.countries = countries;
this.totalResult = totalResult;
this.elementsPerPage = elementsPerPage;
this.pageNumber = pageNumber;
}
}
关于c# - 写this.propertyName或只是propertyName之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23040968/