简体   繁体   English

为什么Visual Studio会提示我不要将`this`关键字用于实例变量?

[英]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 优先顺序

  1. Local variable 局部变量
  2. Instance member 实例成员
  3. Type (static) member 键入(静态)成员

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.

相关问题 为什么语法不突出关键字(Visual Studio 语言服务器) - Why grammar does not highlight keyword (Visual Studio Language Server) 为什么Visual Studio告诉我数据库不存在而存在? - Why does Visual Studio tell me that a database exists when it does not? 在Visual Studio中着色实例变量 - Coloring instance variables in Visual Studio 为什么Visual Studio会给我这种预期的错误类型 - Why does visual studio give me this error type expected 为什么 Visual Studio 告诉我 AddJsonFile() 方法没有定义? - Why does Visual Studio tell me that the AddJsonFile() method is not defined? 为什么当我想通过 C# 运行 GAMS 代码时,visual studio 给我错误? - Why does visual studio gives me error when I want to run the GAMS code via C#? 为什么Visual Studio 2010告诉我“'System.Delegate'不包含'EndInvoke'的定义”? - Why is Visual Studio 2010 telling me “'System.Delegate' does not contain a definition for 'EndInvoke'”? .NET c#:为什么Visual Studio希望我将文件放入c:\\…\\ myConsoleApplication \\ bin \\ Debug - .NET c#: Why does Visual studio expect me to put my files in c:\…\myConsoleApplication\bin\Debug 为什么在我构建项目时Visual Studio没有在Test Explorer中给我测试? - Why Does Visual Studio not give me a test in Test explorer when I build my project? Visual Studio C#不允许我使用Microsoft.AspNet.Identity命名空间 - Visual Studio C# does not allow me to use Microsoft.AspNet.Identity namespace
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM