如何在C#中禁用隐含的“this”?

时间:2022-10-30 09:18:30

This bothers me a lot and I find I write stupid bugs when combined with Intellisense (VS 2008 Pro):

这困扰了我很多,我发现当与Intellisense(VS 2008 Pro)结合使用时,我会写出愚蠢的错误:

class Foo
{
    public Foo(bool isAction)
    {
        this.IsAction = IsAction;
    }

    public bool IsAction { get; private set; }
}

Did you catch it? I certainly didn't until IsAction never changed, causing bugs.

你懂了吗?我当然没有,直到IsAction从未改变,导致错误。

Intellisense somehow converted "isA<tab>" to "IsAction" for me which means the property Foo.IsAction is always false regardless of the constructor input. Just brilliant.

Intellisense以某种方式将“isA ”转换为“IsAction”,这意味着无论构造函数输入如何,属性Foo.IsAction始终为false。真是太棒了

I have to say that I particularly hate the "implicit this" (I don't know if that has a formal name) and I would like to turn it off so it never assumes it. Is there a way to do this? This also applies in calling static methods of the same class.

我不得不说我特别讨厌“暗示这个”(我不知道它是否有一个正式名称)我想把它关掉所以它永远不会假设它。有没有办法做到这一点?这也适用于调用同一类的静态方法。

Alternatively, what naming conventions avoid this little problem? The property must remain "IsAction" so it has to be a convention on the constructor parameter name. Oddly enough, if I name it with the exact matching spelling then this.IsAction = IsAction; works out correctly.

或者,什么命名约定避免这个小问题?该属性必须保持“IsAction”,因此它必须是构造函数参数名称的约定。奇怪的是,如果我用完全匹配的拼写命名它,那么this.IsAction = IsAction;工作得当。

The problem isn't case-sensitive languages but the implicitness of this. Now that I think about it, this also more of a VS 2008 Pro question than a C#. I can live with code already written without the this but I don't want to write new code without it which means telling In

问题不是区分大小写的语言,而是隐含的。现在我考虑一下,这也是一个VS 2008 Pro问题而不是C#。我可以使用已经没有这个编写的代码,但我不想写没有它的新代码,这意味着告诉In


Noldorin's answer got me thinking.

Noldorin的回答让我思考。

Now that I think about it, this also more of a VS 2008 question than a C#. I can live with code already written without the this (though I do change it if I'm in there mucking around) but I don't want to write new code without it which means telling Intellisense to stop doing it. Can I tell Intellisense to knock it off?

现在我考虑一下,这也是一个VS 2008问题而不是C#。我可以使用已经编写过的代码而没有这个(虽然如果我在那里,我会改变它)但我不想写没有它的新代码,这意味着告诉Intellisense停止这样做。我可以告诉Intellisense将它击败吗?

10 个解决方案

#1


This is a common problem. Microsoft has some recommendations for parameter names but they aren't terribly helpful in your case.

这是一个常见问题。 Microsoft对参数名称有一些建议,但它们对您的情况并不十分有用。

As other responders have mentioned, you cannot "disable" the C# language scope resolution behavior - your best approach is a naming conventions. Others have mentioned "Hungarian" notation - some people have a knee-jerk reaction to this because of the confusion over the original intent of the notation.

正如其他响应者所提到的,您不能“禁用”C#语言范围解析行为 - 您最好的方法是命名约定。其他人提到了“匈牙利语”的表示法 - 有些人对此有一种下意识的反应,因为对符号的原始意图的混淆。

My personal approach, has been to use the character 'p' as a prefix to parameter names of public functions. It's unobtrusive, simple, readily identifiable, and easy to enforce with tools like Resharper.

我个人的方法是使用字符'p'作为公共函数参数名称的前缀。它不显眼,简单,易于识别,并且易于使用Resharper等工具强制执行。

The particular naming convention you choose is a matter of preference and style; however, there is some benefit from being consistent in the practice you select.

您选择的特定命名约定是偏好和风格的问题;但是,在您选择的实践中保持一致会带来一些好处。

Using my suggested naming convention, you would write your constructor to:

使用我建议的命名约定,您可以将构造函数编写为:

class Foo
{
    public Foo(bool pIsAction)
    {
        this.IsAction = pIsAction;
    }

    public bool IsAction { get; private set; }
}

#2


I've just tried your code in Visual Studio 2008. Turning on the in built static analysis yields the following error:

我刚刚在Visual Studio 2008中尝试过您的代码。启用内置的静态分析会产生以下错误:

Warning 3 CA1801 : Microsoft.Usage : Parameter 'isAction' of 'Foo.Foo(bool)' is never used. Remove the parameter or use it in the method body.

警告3 CA1801:Microsoft.Usage:从不使用'Foo.Foo(bool)'的参数'isAction'。删除参数或在方法体中使用它。

My suggestion is by turning this on you will find errors like this early on. To enable this choose properties from the context menu on the project, then select the Code Analysis tab and select "Enable Code Analysis on Build"

我的建议是通过启用此功能,您会在早期发现这样的错误。要启用此选项,请从项目的上下文菜单中选择属性,然后选择“代码分析”选项卡并选择“在构建时启用代码分析”

#3


You could always go back to Hungarian notation [I'm preparing to get flamed as I type this]. If you can deal with the ugliness, it would solve your problem. This is a suggestion, not a reccomendation.

你总是可以回到匈牙利表示法[我正准备在我打字的时候受到抨击]。如果你能处理丑陋,那就可以解决你的问题。这是一个建议,而不是推荐。

Alternately, I'm pretty gosh darned sure that static code analysis will catch this and warn you about it. Try FxCop.

或者,我非常认为静态代码分析会抓住这个并警告你。试试FxCop。

EDIT

I have been using Resharper for over a year now, and I do know that it is very smart about assisting you in a case sensitive way. Among other benefits, your intellisense problem will be solved by installing Resharper.

我已经使用Resharper超过一年了,而且我知道以一种区分大小写的方式协助你是非常聪明的。除了其他好处之外,您的intellisense问题将通过安装Resharper来解决。

EDIT 2

I just checked. Neither FxCop, nor Resharper catches this error directly. What both do catch is the fact that the isAction parameter is unused in the Foo method. In this case, the warning would clue you in to the mistake. In cases where the parameter is used in another way within the method, it might slip through static code analysis.

我刚刚检查过FxCop和Resharper都没有直接捕获此错误。两者都捕获的是事实,即在Foo方法中未使用isAction参数。在这种情况下,警告会让你知道错误。如果在方法中以另一种方式使用参数,它可能会通过静态代码分析。

#4


FxCop will complain about this because the isAction parameter is never used. Specifically it will pull rule CA1801:ReviewUnusedParameters.

FxCop会抱怨这个,因为从不使用isAction参数。具体来说,它将拉出规则CA1801:ReviewUnusedParameters。

Personally I've always felt that the C# compiler should give a warning about unused parameters.

就个人而言,我一直认为C#编译器应该对未使用的参数发出警告。

#5


This gets me all the time. I've taken to prepending variables being passed into the constructor with a '_', like:

这让我一直都在。我已经采用“_”来预先添加传递给构造函数的变量,如:

class Foo
{    
    public Foo(bool _isAction)
    {
        this.IsAction = _isAction;
    }
    public bool IsAction { get; private set; }}

#6


I'm afraid there's no way to disable the "implicit this" feature. It's part of the language specification and compiler, and there's no way to turn it off.

我担心没有办法禁用“隐含此功能”。它是语言规范和编译器的一部分,并且无法将其关闭。

Personally, I don't consider this a big problem. Admittedly, it is important to be careful with the capitalisation of member and parameter names, but this is always the situation in a case-sensitive language such as C#.

就个人而言,我不认为这是一个大问题。不可否认,要小心成员和参数名称的大写是很重要的,但这始终是一种区分大小写的语言,例如C#。

My recommended "solution" (which you already seem to be doing) is to always use the this keyword for referencing properties/fields, so that it should immediately stand out when you should be using a parameter. It's not going to solve the problem for you, but if you keep it in mind it will undoubtedly help. Just getting in the habit of this (as well as remembering all parameter/local variable names should start with lower-case) will do you well to avoid this issue in the future.

我推荐的“解决方案”(你似乎已经在做)是始终使用this关键字来引用属性/字段,这样当你应该使用参数时它应该立即脱颖而出。它不会为你解决问题,但如果你牢记它,它无疑会有所帮助。养成这个习惯(以及记住所有参数/局部变量名称应该从小写开始)将会很好地避免将来出现这个问题。

#7


It's an annoying problem in Visual Studio's intellisense. Resharper gets it right most of the time though.

这是Visual Studio智能感知中的烦人问题。 Resharper大部分时间都是正确的。

#8


I think it's more likely that the developer picked "IsAction" instead of "isAction" from Intellisense. I don't think Intellisense would change "isA" to "this.IsAction".

我认为开发人员更有可能从Intellisense选择“IsAction”而不是“isAction”。我认为Intellisense不会将“isA”改为“this.IsAction”。

If the names differ only by case then I think the only way to prevent mistakes like this is by being aware of them and careful, and by using unit tests effectively.

如果名称只是大小写不同,那么我认为防止这样的错误的唯一方法是通过了解它们并小心,并有效地使用单元测试。

#9


It might be annoying in other capacities, but you could turn off the option to have Intellisense pre-select the most recently used member. I realize that won't really fix the problem outright but it might possibly help prevent some accidental tabs when you don't really have the right item selected.

它可能在其他容量上很烦人,但您可以关闭Intellisense预先选择最近使用的成员的选项。我意识到这并不能完全解决这个问题,但是当你没有选择正确的项目时,它可能有助于防止一些意外的标签。

#10


Note:

If you are using Resharper, you have some shortcuts that write a lot of this code for you and avoid the error.

如果您正在使用Resharper,您有一些快捷方式可以为您编写大量此代码并避免错误。

You can either create the property first, then hit Alt-Ins and choose "generate constructor", or you can add a parameter "isAction" to the constructor, hit Alt-Enter with the caret in the parameterName, and choose the "Create and initialize Auto-property IsAction" action from the menu that pops up.

你可以先创建属性,然后点击Alt-Ins并选择“生成构造函数”,或者你可以在构造函数中添加一个参数“isAction”,在参数名称中用插入符号按Alt-Enter,然后选择“创建和从弹出的菜单中初始化“自动属性IsAction”操作。

#1


This is a common problem. Microsoft has some recommendations for parameter names but they aren't terribly helpful in your case.

这是一个常见问题。 Microsoft对参数名称有一些建议,但它们对您的情况并不十分有用。

As other responders have mentioned, you cannot "disable" the C# language scope resolution behavior - your best approach is a naming conventions. Others have mentioned "Hungarian" notation - some people have a knee-jerk reaction to this because of the confusion over the original intent of the notation.

正如其他响应者所提到的,您不能“禁用”C#语言范围解析行为 - 您最好的方法是命名约定。其他人提到了“匈牙利语”的表示法 - 有些人对此有一种下意识的反应,因为对符号的原始意图的混淆。

My personal approach, has been to use the character 'p' as a prefix to parameter names of public functions. It's unobtrusive, simple, readily identifiable, and easy to enforce with tools like Resharper.

我个人的方法是使用字符'p'作为公共函数参数名称的前缀。它不显眼,简单,易于识别,并且易于使用Resharper等工具强制执行。

The particular naming convention you choose is a matter of preference and style; however, there is some benefit from being consistent in the practice you select.

您选择的特定命名约定是偏好和风格的问题;但是,在您选择的实践中保持一致会带来一些好处。

Using my suggested naming convention, you would write your constructor to:

使用我建议的命名约定,您可以将构造函数编写为:

class Foo
{
    public Foo(bool pIsAction)
    {
        this.IsAction = pIsAction;
    }

    public bool IsAction { get; private set; }
}

#2


I've just tried your code in Visual Studio 2008. Turning on the in built static analysis yields the following error:

我刚刚在Visual Studio 2008中尝试过您的代码。启用内置的静态分析会产生以下错误:

Warning 3 CA1801 : Microsoft.Usage : Parameter 'isAction' of 'Foo.Foo(bool)' is never used. Remove the parameter or use it in the method body.

警告3 CA1801:Microsoft.Usage:从不使用'Foo.Foo(bool)'的参数'isAction'。删除参数或在方法体中使用它。

My suggestion is by turning this on you will find errors like this early on. To enable this choose properties from the context menu on the project, then select the Code Analysis tab and select "Enable Code Analysis on Build"

我的建议是通过启用此功能,您会在早期发现这样的错误。要启用此选项,请从项目的上下文菜单中选择属性,然后选择“代码分析”选项卡并选择“在构建时启用代码分析”

#3


You could always go back to Hungarian notation [I'm preparing to get flamed as I type this]. If you can deal with the ugliness, it would solve your problem. This is a suggestion, not a reccomendation.

你总是可以回到匈牙利表示法[我正准备在我打字的时候受到抨击]。如果你能处理丑陋,那就可以解决你的问题。这是一个建议,而不是推荐。

Alternately, I'm pretty gosh darned sure that static code analysis will catch this and warn you about it. Try FxCop.

或者,我非常认为静态代码分析会抓住这个并警告你。试试FxCop。

EDIT

I have been using Resharper for over a year now, and I do know that it is very smart about assisting you in a case sensitive way. Among other benefits, your intellisense problem will be solved by installing Resharper.

我已经使用Resharper超过一年了,而且我知道以一种区分大小写的方式协助你是非常聪明的。除了其他好处之外,您的intellisense问题将通过安装Resharper来解决。

EDIT 2

I just checked. Neither FxCop, nor Resharper catches this error directly. What both do catch is the fact that the isAction parameter is unused in the Foo method. In this case, the warning would clue you in to the mistake. In cases where the parameter is used in another way within the method, it might slip through static code analysis.

我刚刚检查过FxCop和Resharper都没有直接捕获此错误。两者都捕获的是事实,即在Foo方法中未使用isAction参数。在这种情况下,警告会让你知道错误。如果在方法中以另一种方式使用参数,它可能会通过静态代码分析。

#4


FxCop will complain about this because the isAction parameter is never used. Specifically it will pull rule CA1801:ReviewUnusedParameters.

FxCop会抱怨这个,因为从不使用isAction参数。具体来说,它将拉出规则CA1801:ReviewUnusedParameters。

Personally I've always felt that the C# compiler should give a warning about unused parameters.

就个人而言,我一直认为C#编译器应该对未使用的参数发出警告。

#5


This gets me all the time. I've taken to prepending variables being passed into the constructor with a '_', like:

这让我一直都在。我已经采用“_”来预先添加传递给构造函数的变量,如:

class Foo
{    
    public Foo(bool _isAction)
    {
        this.IsAction = _isAction;
    }
    public bool IsAction { get; private set; }}

#6


I'm afraid there's no way to disable the "implicit this" feature. It's part of the language specification and compiler, and there's no way to turn it off.

我担心没有办法禁用“隐含此功能”。它是语言规范和编译器的一部分,并且无法将其关闭。

Personally, I don't consider this a big problem. Admittedly, it is important to be careful with the capitalisation of member and parameter names, but this is always the situation in a case-sensitive language such as C#.

就个人而言,我不认为这是一个大问题。不可否认,要小心成员和参数名称的大写是很重要的,但这始终是一种区分大小写的语言,例如C#。

My recommended "solution" (which you already seem to be doing) is to always use the this keyword for referencing properties/fields, so that it should immediately stand out when you should be using a parameter. It's not going to solve the problem for you, but if you keep it in mind it will undoubtedly help. Just getting in the habit of this (as well as remembering all parameter/local variable names should start with lower-case) will do you well to avoid this issue in the future.

我推荐的“解决方案”(你似乎已经在做)是始终使用this关键字来引用属性/字段,这样当你应该使用参数时它应该立即脱颖而出。它不会为你解决问题,但如果你牢记它,它无疑会有所帮助。养成这个习惯(以及记住所有参数/局部变量名称应该从小写开始)将会很好地避免将来出现这个问题。

#7


It's an annoying problem in Visual Studio's intellisense. Resharper gets it right most of the time though.

这是Visual Studio智能感知中的烦人问题。 Resharper大部分时间都是正确的。

#8


I think it's more likely that the developer picked "IsAction" instead of "isAction" from Intellisense. I don't think Intellisense would change "isA" to "this.IsAction".

我认为开发人员更有可能从Intellisense选择“IsAction”而不是“isAction”。我认为Intellisense不会将“isA”改为“this.IsAction”。

If the names differ only by case then I think the only way to prevent mistakes like this is by being aware of them and careful, and by using unit tests effectively.

如果名称只是大小写不同,那么我认为防止这样的错误的唯一方法是通过了解它们并小心,并有效地使用单元测试。

#9


It might be annoying in other capacities, but you could turn off the option to have Intellisense pre-select the most recently used member. I realize that won't really fix the problem outright but it might possibly help prevent some accidental tabs when you don't really have the right item selected.

它可能在其他容量上很烦人,但您可以关闭Intellisense预先选择最近使用的成员的选项。我意识到这并不能完全解决这个问题,但是当你没有选择正确的项目时,它可能有助于防止一些意外的标签。

#10


Note:

If you are using Resharper, you have some shortcuts that write a lot of this code for you and avoid the error.

如果您正在使用Resharper,您有一些快捷方式可以为您编写大量此代码并避免错误。

You can either create the property first, then hit Alt-Ins and choose "generate constructor", or you can add a parameter "isAction" to the constructor, hit Alt-Enter with the caret in the parameterName, and choose the "Create and initialize Auto-property IsAction" action from the menu that pops up.

你可以先创建属性,然后点击Alt-Ins并选择“生成构造函数”,或者你可以在构造函数中添加一个参数“isAction”,在参数名称中用插入符号按Alt-Enter,然后选择“创建和从弹出的菜单中初始化“自动属性IsAction”操作。