C# 通过使用关键字this,构造函数的实现有什么不同。?

C# 通过使用关键字this,构造函数的实现有什么不同。?,c#,constructor,reference,C#,Constructor,Reference,请您解释一下这两种构造函数实现之间的区别: public User(string a, string b) { name = a; location = b; } 还有这个: public User(string a, string b) { this.name = a; this.location = b; } 从编译器的角度来看,我看不出有什么不同。请解释一下。没有区别

请您解释一下这两种构造函数实现之间的区别:

 public User(string a, string b)

    {

        name = a;

        location = b;

    }
还有这个:

  public User(string a, string b)

    {

        this.name = a;

        this.location = b;

    }

从编译器的角度来看,我看不出有什么不同。请解释一下。

没有区别

仅引用类,如果传入的参数与类中的字段具有相同的名称(以区别于它们),则此选项很有用


其他资源

this关键字引用类的当前实例,是 也用作扩展方法的第一个参数的修饰符


没有区别。但是,如果参数名恰好与指定的字段或属性名相同,您必须使用
this.
来消除赋值的歧义。可能重复的是,对不同的变量和构造函数参数也使用“this”是好的做法吗?或者如果它们只是相同的话?@Heron我不使用
this
在这种情况下,有些人喜欢它,因为它很突出,这取决于您喜欢如何编写代码,有些人喜欢以下划线开头的私有字段名。ie
\u字段
有些字段不需要,如果这对您有帮助,那么请保持一致,就我个人而言,我使用下划线作为字段名称,它们会突出
public class Employee
{
    private string alias;
    private string name;

    public Employee(string name, string alias)
    {
        // Use this to qualify the members of the class 
        // instead of the constructor parameters.
        this.name = name;
        this.alias = alias;
    }
}