C# 编写此.propertyName与仅编写propertyName的区别

C# 编写此.propertyName与仅编写propertyName的区别,c#,constructor,C#,Constructor,我正在看一个编写类并使用构造函数的示例,我想知道使用“this.”和不使用“this.”有什么区别 那么,以下两者之间的区别是什么: public class PagedCountryList { public IEnumerable<Country> CountriesToDisplay { get; set; } public int Total { get; set; } public int PerPage { get; set; } publ

我正在看一个编写类并使用构造函数的示例,我想知道使用“this.”和不使用“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;
    }
}
公共类PagedCountryList
{
公共IEnumerable CountriesToDisplay{get;set;}
公共整数总计{get;set;}
公共整型每页{get;set;}
公共整数页码{get;set;}
public PagedCountryList(IEnumerable国家/地区、int totalResult、int elementsPerPage、int pageNumber)
{
this.CountriesToDisplay=国家;
这个.Total=totalResult;
this.PerPage=elementsPerPage;
this.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;
    }
}
公共类PagedCountryList
{
公共IEnumerable CountriesToDisplay{get;set;}
公共整数总计{get;set;}
公共整型每页{get;set;}
公共整数页码{get;set;}
public PagedCountryList(IEnumerable国家/地区、int totalResult、int elementsPerPage、int pageNumber)
{
CountriesToDisplay=国家;
总计=总计结果;
每个页面=元素页面;
页码=页码;
}
}

在您的情况下,没有区别。这是指你自己。
在某些情况下,如果在同一范围内您有“重复命名变量”,则此选项非常有用。

在您的情况下,没有任何差异。这是指你自己。
有用的是,在相同的情况下,你有“重复命名变量”。

在你的情况下没有差异,但是考虑这个例子

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;
    }
}
公共类PagedCountryList
{
私有IEnumerable国家{get;set;}
公共整数totalResult{get;set;}
public int elementsPerPage{get;set;}
公共整数页码{get;set;}
public PagedCountryList(IEnumerable国家/地区、int totalResult、int elementsPerPage、int pageNumber)
{
//你不能写这样的东西(因为它模棱两可)
//国家=国家;
这个国家=国家;
this.totalResult=totalResult;
this.elementsPerPage=elementsPerPage;
this.pageNumber=页码;
}
}

>P>你的情况没有差异,但是考虑这个例子

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;
    }
}
公共类PagedCountryList
{
私有IEnumerable国家{get;set;}
公共整数totalResult{get;set;}
public int elementsPerPage{get;set;}
公共整数页码{get;set;}
public PagedCountryList(IEnumerable国家/地区、int totalResult、int elementsPerPage、int pageNumber)
{
//你不能写这样的东西(因为它模棱两可)
//国家=国家;
这个国家=国家;
this.totalResult=totalResult;
this.elementsPerPage=elementsPerPage;
this.pageNumber=页码;
}
}

正如tom所建议的,在这种情况下,“this”是多余的,但这并不意味着你不应该使用它

您的成员“public int Total”可以使用“this”或不使用“this”直接访问。另一方面,如果使用Total作为函数参数,则需要用“this”区分它是否是函数参数的类成员


正如汤姆所说,在这种情况下,“这”是多余的,但这并不意味着你不应该使用它

您的成员“public int Total”可以使用“this”或不使用“this”直接访问。另一方面,如果使用Total作为函数参数,则需要用“this”区分它是否是函数参数的类成员


事实上,你的情况没有什么不同。下面是从中描述的
的常用用法

  • 限定以相似名称隐藏的成员的步骤
  • 将对象作为参数传递给其他方法的步骤
  • 声明索引器

事实上,你的情况没有什么不同。下面是从中描述的
的常用用法

  • 限定以相似名称隐藏的成员的步骤
  • 将对象作为参数传递给其他方法的步骤
  • 声明索引器

没有区别。”在这种情况下,这是多余的。
如果构造函数采用与属性同名的参数,则需要此
。感谢您的快速回答!可能有用——没什么区别。”在这种情况下,这是多余的。
如果构造函数采用与属性同名的参数,则需要此
。感谢您的快速回答!可能有用-