【发布时间】:2009-06-26 18:09:48
【问题描述】:
可能的重复:
When do you use the “this” keyword?
Best practices for using the ‘this’ keyword in C#
我有一个关于 this 关键字的样式问题。为了清楚起见,在类中自引用自动实现的属性或方法时,您是否使用 this?
例如,在您的构造函数中,您是否将参数赋值写为:
public class Foo
{
public string FooProperty { get; set; }
public Foo(string fooProperty)
{
this.FooProperty = fooProperty;
}
...
}
或为:
public class Foo
{
public string FooProperty { get; set; }
public Foo(string fooProperty)
{
FooProperty = fooProperty;
}
...
}
【问题讨论】:
-
我不会对此投反对票。 SO 搜索框无法正确找到“this”。
-
tvanfosson:这实际上是一个副本的副本。 :)
-
是的,当然没有必要对此投反对票,因为这实际上是一个很好的问题,只是两个都得到了回答。
标签: c# .net coding-style