请你解释一下这两个构造函数的实现有什么区别:
public User(string a, string b)
{
name = a;
location = b;
}
还有这个:
public User(string a, string b)
{
this.name = a;
this.location = b;
}
从编译器的角度来看,我没有看到任何区别。请解释一下。
最佳答案
没有区别,
this
只是引用类,如果您传入的参数与类中的字段具有相同的名称(以区分它们),那么它很有用
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;
}
}
其他资源
The this keyword refers to the current instance of the class and is also used as a modifier of the first parameter of an extension method
关于c# - 使用关键字this.实现构造函数有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53020197/