简体   繁体   English

我可以在代码中强制使用`this`吗?

[英]Can I make using `this` mandatory in my code?

Is there a way to tell Visual Studio to make qualifying members of the current instance with this. 有没有办法告诉Visual Studio以此使当前实例的合格成员this. mandatory? 强制性? I prefer the clarity. 我希望清楚。

Details: 细节:

In C#, if you have instance members (fields, properties, methods, etc.), normally you can refer to them both with and without this , eg: 在C#中,如果您有实例成员(字段,属性,方法等),通常可以使用this或不使用this来引用它们,例如:

class Foo
{
    private int bar;

    Foo()
    {
        // These two lines are both valid, and both do exactly the same thing
        bar = 42;
        this.bar = 42;
    }
}

In my coding style, I don't want this to optional, I want the first line above to cause at least a warning. 在我的编码风格中,我不想this为可选,我希望上面的第一行至少引起警告。 Obviously this is just for my own purposes, I'm not trying to mark the resulting assembly in some way that requires people using it to also do this, this is purely a personal style thing. 显然,这只是出于我自己的目的,我并没有试图以某种方式标记生成的程序集,这要求人们使用它也要执行此操作,这纯粹是个人风格的事情。

You could use programs such as Resharper (using the setting described here ) and StyleCop (using this rule ). 您可以使用诸如Resharper (使用此处描述的设置 )和StyleCop (使用此规则 )之类的程序。

They allow you to define your own coding styles which cause validation warning in your code with suggestions on who to resolve them. 它们允许您定义自己的编码样式,这些样式会在代码中引起验证警告,并提供有关解决它们的建议。

Here is a question on free alternatives to resharper. 这是关于免费的替代工具的问题。

What are some alternatives to resharper? 有什么替代方法?

StyleCop似乎默认情况下启用了此规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM