在 c# Visual Studio vs Stylecop 中使用这个关键字 [英] Using this keyword in c# Visual studio vs Stylecop

查看:28
本文介绍了在 c# Visual Studio vs Stylecop 中使用这个关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Windows 服务 C# 项目,并且我是第一次使用 C#.

I am working on a windows service C# project and I am working first time in C#.

对于相同的 this 关键字,我从 VS 以及 Stylecop 那里得到了 2 个不同的建议.(我知道,this 关键字指的是类的当前实例)

An I am getting 2 different suggestion from VS as well Stylecop for a same this keyword. (I know, this keyword refers to the current instance of the class)

所以我删除了这个关键字,我得到了 stylecop 的建议

So I removed this keyword, and I am getting stylecop suggestion as

SA1101:CSharp.Readability:对 serializeBackupFilePath 的调用必须以这个"开头.前缀表示该项目是班级.

SA1101 : CSharp.Readability : The call to serializeBackupFilePath must begin with the 'this.' prefix to indicate that the item is a member of the class.

所以,我在这里很困惑,所以有 this 关键字的构造函数和没有 this 关键字的构造函数之间有什么区别(抱歉我的英语不好)

So, I am confused here, so is there will have any difference between constructor having this keyword and constructor without this keyword (Sorry for my bad english)

推荐答案

我不喜欢使用 this.但是,由于您对参数和类成员使用相同的命名约定,如果没有它,可能很难区分这两者.

I prefer not using this. However, since you are using the same naming convention for arguments and class members, it might be hard to differentiate between those two without it.

由于您使用的是 C#,我建议您使用此命名约定,以避免您需要使用 this 来明确指定您的意思:

Since you are using C# I'd recommend this naming convention to avoid cases where you need to use this to explicitly specify which one you mean:

  • _name 用于私人班级成员
  • Name 用于公共类成员、常量和方法名称
  • name 用于参数和局部变量
  • _name for private class members
  • Name for public class members, constants and method names
  • name for arguments and local variables

StyleCop 对 SA1101 的官方描述是:

StyleCop's official description for SA1101 is:

每当代码包含对本地类或基类的实例成员的调用时,就会违反此规则.当存在基类成员的本地覆盖时,会出现此规则的例外,并且代码打算直接调用基类成员,绕过本地覆盖.在这种情况下,调用的前缀可以是base."而不是this.".

A violation of this rule occurs whenever the code contains a call to an instance member of the local class or a base class which is not prefixed with ‘this.’. An exception to this rule occurs when there is a local override of a base class member, and the code intends to call the base class member directly, bypassing the local override. In this case the call can be prefixed with ‘base.’ rather than ‘this.’.

默认情况下,StyleCop 不允许使用下划线或 m_ 来标记本地类字段,而支持this."前缀.使用this."的好处是它同样适用于所有元素类型,包括方法、属性等,而不仅仅是字段,使得所有对类成员的调用都可以立即识别,无论使用哪个编辑器来查看代码.另一个优点是它在实例成员和没有前缀的静态成员之间创建了快速、可识别的区别.

By default, StyleCop disallows the use of underscores or m_ to mark local class fields, in favor of the ‘this.’ prefix. The advantage of using ‘this.’ is that it applies equally to all element types including methods, properties, etc., and not just fields, making all calls to class members instantly recognizable, regardless of which editor is being used to view the code. Another advantage is that it creates a quick, recognizable differentiation between instance members and static members, which are not be prefixed.

使用this."前缀的最后一个好处是可以输入 this.将使 Visual Studio 显示 IntelliSense 弹出窗口,使开发人员可以快速轻松地选择要调用的类成员.

A final advantage of using the ‘this.’ prefix is that typing this. will cause Visual Studio to show the IntelliSense popup, making it quick and easy for the developer to choose the class member to call.

我完全不同意这一点,但也许它可以帮助您决定是否使用 this.

I disagree completely with that, but maybe it helps you decide wether to use this or not.

顺便说一句:这已经在 StyleCop 的 Github 上讨论过.
官方建议禁用SA1101.

这篇关于在 c# Visual Studio vs Stylecop 中使用这个关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆