为什么VS使用“this”这么频繁?

发布于 2024-10-20 18:23:29 字数 88 浏览 2 评论 0原文

我知道“this”关键字的用途,但不明白为什么 VS 几乎总是使用它,比如 this.Invalidate。如果调用放在类内部,并且标识符没有问题,那有什么用呢?

I know what "this" keyword serves for but do not udnerstand why VS uses it nearly always, like this.Invalidate. If the call is placed within the class and there is no problem with identifier, what is it good for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

隱形的亼 2024-10-27 18:23:29

使用它的不是 VS,而是开发人员。如果您使用像 Resharper 这样的工具,它总是会指出“this”在哪里是不必要或多余的。

It is not VS who uses it but the developers who do. If you use a tool like Resharper, it always points out where "this" is unnecessary or redundant.

心安伴我暖 2024-10-27 18:23:29

我认为这主要是风格问题。使用 this 引用可以 100% 清楚您要引用的内容。

您说标识符没有问题(因为范围内不存在名称冲突),但这并不意味着将来不会出现问题。

可以将其他名称或类添加到外部范围。这不太可能,也可能不会造成问题,但它有助于消除任何歧义的可能性。

I think it's mainly a question of style. Using the this reference makes it 100% clear what you intend to reference.

You say there is no problem with the identifier (in that there is no clash of names in scope), but that doesn't mean there won't be in the future.

Additional names or classes could be added to an outer scope. It's unlikely and probably wouldn't cause a problem anyway, but it helps to remove any possibility of ambiguity.

浮华 2024-10-27 18:23:29

它明确表明这个变量或方法属于这个类。它更具可读性。但这不是强制性的。由开发人员和团队决定是否要以这种方式使用它。这只是另一种代码风格。

It explicitly shows that this variable or method belongs to this class. It more readable. Its not mandatory though. Its up to the developer and the team to decide whether they want to use it this way or not. It's just another code style.

无言温柔 2024-10-27 18:23:29

如果调用放在类内部,并且标识符没有问题,那有什么用呢?

我假设您谈论的是自动生成的代码,例如来自表单设计器的代码。它可以

a) 被编写为分析它已经发出的代码,确定标识符是否不明确,然后基于此,决定是否使用 this. 来正确限定名称,或者,

b)只需将 this. 用于所有类范围的标识符即可。


在许多情况下,它似乎是多余的,但无论您是否在明确的上下文中使用 this. ,都会生成完全相同的 IL 代码,因此始终包含它不会造成任何损害。

If the call is placed within the class and there is no problem with identifier, what is it good for?

I assume your talking about autogenerated code, from e.g. the forms designer. It could

a) be written to analyse the code it's already emitted, decide whether the identifier is ambiguous, and then based on that, decide whether to use this. to properly qualify the name, or,

b) just use this. for all class-scoped identifiers.


It may seem redundant in many cases, but the exact same IL code will be produced whether or not you use this. in an unambiguous context, so it's not as if it's causing any harm to always include it.

笛声青案梦长安 2024-10-27 18:23:29

在不必要的情况下添加 this 可以降低代码的复杂性并使其更具可读性。

Adding this, in unnecessary cases, reduces your code's complexity and makes it more readable.

国际总奸 2024-10-27 18:23:29

哈哈哈,因为如果你用“这个”。然后智能感知就会弹出,所有班级成员都可以供你使用,而不必输入太多内容(或犯很多打字错误,无论如何......)

Hahaha, because if you use "this." then Intellisense pops up and you have all your class members at your disposal and don't have to type so much (or make so many typos, whatever...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文