[英]Why does Visual Studio prompt me NOT to use the `this` keyword for instance variables?
With a class as defined below, I would expect that I would need to explicitly state instance variables by prefixing them with 'this'. 对于下面定义的类,我希望我需要通过在它们前面加上'this'来显式声明实例变量。 Coming from a Ruby and Javascript background, I was expecting that
description
would need to be prefixed with this
at the declaration, and that within the constructor, that this
would be required. 从一个Ruby和JavaScript背景的人,我期待这
description
将需要前缀this
在申报,并在构造函数中,那this
将是必需的。
Why is it not required? 为什么不需要? I assume that
description
is still getting created as an instance variable? 我假设
description
仍然是作为实例变量创建的?
public class Item
{
private string description;
public Item(string str)
{
this.description = str; // VS says the 'this' keyword can be omitted
}
}
Because there is no conflicting locally scoped description variable and in that case the type or instance member is used. 因为没有冲突的本地范围的描述变量,在这种情况下使用类型或实例成员。
Priority order 优先顺序
So if you had a local variable also named description
but you wanted to reference the instance member then this
would be required otherwise you would always reference the the local variable. 因此,如果您有一个名为
description
的局部变量,但您想引用实例成员,那么this
将是必需的,否则您将始终引用局部变量。
Here is an example where you should use this
这是一个你应该使用
this
的例子
public class Item
{
private string description;
public void SetDescription(string description)
{
this.description = description; // without this you would just be setting the local variable to itself
}
}
You're asking two different questions. 你问的是两个不同的问题。
The first one is, why don't you need this
. 第一个是,你为什么不需要
this
。
The reason for that is that C# is a statically typed language. 原因是C#是一种静态类型语言。 In javascript, you can say
foo = 7
, and a variable foo
will start to exist. 在javascript中,你可以说
foo = 7
,变量foo
将开始存在。
In C#, you can't do that. 在C#中,你无法做到这一点。
description
refers to this.description
, and in this context can't refer to anything else. description
是指this.description
,在此上下文中不能引用其他任何内容。
Your second question is why does Visual Studio tell you not to do that. 您的第二个问题是为什么Visual Studio会告诉您不要这样做。 The answer to that is that notifying you is its default code style.
答案就是通知你的默认代码风格。
You can change this behaviour in your editor settings. 您可以在编辑器设置中更改此行为。 You can choose what you want your codestyle to be (prefer
this
, prefer not this
, and either inform or warn when you do something else). 你可以选择你想要的代码风格(更喜欢
this
,不喜欢this
,并在你做其他事情时通知或警告)。
There is nothing inherently wrong in preferring one style over the other, but it's a good idea to be consistent. 选择一种风格而不是另一种风格没有任何内在错误,但保持一致是个好主意。 Visual Studio provides you the tools to be consistent in the style you choose.
Visual Studio为您提供了所选样式的一致性工具。
It is definitely not required, but it is correct so in the end is up to you if you want to use it or not. 它绝对不是必需的,但它是正确的,所以如果你想使用或不使用它最终取决于你。
I use it mainly because it's an indication that whatever property or method it's used it, I know it's inside that class so there is no ambiguity. 我之所以使用它主要是因为它表明它使用它的任何属性或方法,我知道它在该类中,所以没有歧义。 Both VS and Resharper will pick up on that but I would say it's a personal preference whether you want to use it or not.
VS和Resharper都会接受,但我想说无论你是否想要使用它都是个人喜好。
Here are some excerpts from the C# Language Specification , which is the authorative source for how C# works (emphasis added): 以下是C#语言规范的一些摘录,它是C#如何工作的强制来源(强调添加):
§3.7: The scope of a member declared by a class-member-declaration (§10.1.6) is the class-body in which the declaration occurs .
§3.7:类成员声明(第10.1.6节)声明的成员范围是声明发生的类主体 。
That means you can access the Item.description
field by its name from all instance methods of Item
. 这意味着您可以从
Item
所有实例方法中按名称访问Item.description
字段。 Because the variable is in scope, you don't need this
. 因为变量在范围内,所以不需要
this
。
§3.7: The scope of a local variable declared in a local-variable-declaration (§8.5.1) is the block in which the declaration occurs .
§3.7:在local-variable-declaration(第8.5.1节)中声明的局部变量的范围是声明发生的块 。
That means you could access a local variable description
within the block it's defined in. 这意味着您可以访问其定义的块中的局部变量
description
。
§3.7.1.1 Name hiding through nesting can occur as a result of nesting namespaces or types within namespaces, as a result of nesting types within classes or structs, and as a result of parameter and local variable declarations .
§3.7.1.1由于在类或结构中嵌套类型以及作为参数和局部变量声明的结果,在命名空间内嵌套命名空间或类型,可能会发生通过嵌套隐藏的名称。
That means that if you define both Item.description
and a local description
. 这意味着如果您同时定义
Item.description
和本地description
。 In this case the local variable hides the instance field. 在这种情况下,局部变量隐藏实例字段。
§7.6.7 When this is used in a primary-expression within an instance method or instance accessor of a class, it is classified as a value .
§7.6.7当在类的实例方法或实例访问器中的primary-expression中使用它时 , 它被分类为值 。 The type of the value is the instance type (§10.3.1) of the class within which the usage occurs, and the value is a reference to the object for which the method or accessor was invoked .
值的类型是发生使用的类的实例类型(第10.3.1节), 值是对调用方法或访问器的对象的引用 。
That means, that this
in C# is not a required keyword to specify instance scope. 这意味着,
this
在C#中不指定实例范围内所需的关键字。 It simply is a value that equals the object the method is called on. 它只是一个值 , 该值等于调用该方法的对象。 That means that
this.description
is semantically equivalent to anyItem.description
as it means accessing the description
field on the object. 这意味着
this.description
在语义上等同于anyItem.description
因为它意味着访问对象上的description
字段。
Putting everything together: 把所有东西放在一起:
When Item.description
is hidden, using this
is the only way of accessing the hidden member. 隐藏
Item.description
,使用this
是访问隐藏成员的唯一方法。 However, if it is not hidden, you can access it because it is in scope. 但是,如果未隐藏,则可以访问它,因为它在范围内。 Hence, the VS recommends to drop the
this
, because it's not needed. 因此,VS建议放弃
this
,因为它不需要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.