使用“值"作为C#中的标识符 [英] Using "value" as an identifier in C#

查看:53
本文介绍了使用“值"作为C#中的标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编写简短的辅助函数时,我经常发现自己想使用变量标识符"value"作为参数.当我这样做时,似乎Visual Studio可以很好地进行编译,并且没有任何抱怨:

In writing short helper functions, I often find myself wanting to use the variable identifier "value" as an argument. It seems as though Visual Studio compiles this just fine, and has no complaints, when I do this:

public void MyMethod(int value, bool option, string message)
{
    value = 1;
    // More code...
}

但是,Visual Studio抱怨以下情况(按预期):

However, Visual Studio complains at the following (as expected):

private int _myProperty;
public int MyProperty
{
    get
    {
        return _myProperty;
    }
    set
    {
        int value = 0;
        _myProperty = value;
    }
}

这使我相信,取决于上下文,值"被视为一个关键字(或不视为关键字).我对C#相当陌生,据我所知,我还没有看到其他语言中的特定于上下文的关键字.

This leads me to believe that "value" is treated as a keyword (or not) depending on the context. I am fairly new to C# and, as far as I know, I have not seen context-specific keywords in other languages.

问题:在属性设置器之外使用值"作为变量名是否总是安全的?如果没有,什么时候可以安全地完成?而且,这通常被认为是不好的做法吗?

The question: Is it always safe to use "value" as a variable name outside of a property setter? If not, when can this be done safely? And, is this often considered bad practice?

令我感到惊讶的是,我无法找到已经在SO上问过的问题,并且我怀疑以前有人问过.但是,由于标题中有变量"和标识符"的帖子太多,因此很难搜索.我无法在MSDN上找到有关此信息.

最后一个问题是询问是否经常 common 皱眉.它已更改以反映这一点.

The last question is meant to ask if it is often or commonly frowned upon. It has been changed to reflect this.

推荐答案

MSDN 说:

set访问器类似于其返回类型为void的方法.它使用一个名为value的隐式参数,其类型是属性的类型.

The set accessor resembles a method whose return type is void. It uses an implicit parameter called value, whose type is the type of the property.

这些属性基本上是语法糖,可以避免您不得不编写大量的 get_Bar set_Bar 方法(注意:CLR也知道其他一些优点)财产).例如,如果您有一个类似这样的课程:

The properties are basically syntactic sugar that avoids you having to write a lot of get_Bar and set_Bar methods (note: there are some other advantages too, the CLR knows it's a property). For example, if you have a class like this:

public class Foo
{
    private int _bar;
    public int Bar
    {
        get { return _bar; }
        set { _bar = value; }
    }
}

它将生成IL(用于setter),如下所示:

It'll generate IL (for the setter) that looks something like this:

.method public hidebysig specialname 
            instance void  set_Bar(int32 'value') cil managed
    {
      // 
      .maxstack  8
      IL_0000:  nop
      IL_0001:  ldarg.0
      IL_0002:  ldarg.1
      IL_0003:  stfld      int32 Program/Foo::_bar
      IL_0008:  ret
    } // end of method Foo::set_Bar

这里要注意的是 set_Bar 方法采用一个名为 value 的参数.因此,它不仅类似于"一个返回类型为void的方法,并带有名为 value 的参数,实际上就是这样.

The thing to note here is that the set_Bar method takes a parameter called value. So not only does it "resemble" a method whose return type is void with a parameter called value, it actually is that.

因此,很明显,您不能在设置器中将 value 用作其他内容.

So you can't use value for something else in a setter, obviously.

现在您应该在其他地方使用它吗?这取决于.如果很明显在您使用它的上下文中它指的是什么,那么可以肯定.如果 value 在特定上下文中不明确,请使用更明确的内容.

Now should you use it elsewhere? It depends. If it's obvious what it's referring to in the context where you are using it then sure. If value is ambiguous in a particular context then use something more explicit.

来自 MSDN :

上下文关键字值在普通属性声明中的set访问器中使用.

The contextual keyword value is used in the set accessor in ordinary property declarations.

它没有提及将 value 视为关键字的任何其他上下文,因此,除了设置器,或其他可能已经定义了的地方之外,您使用 value 应该可以.这是不好的做法吗?通常,不超过任何其他可能含糊的变量名.

It makes no mention of any other context where value is considered a keyword, so aside from a setter, or anywhere else where it might have been defined already, you should be fine using value. Is it bad practice? Not as a rule, no more than any other potentially ambiguous variable name.

我认为以 value 作为名称的确很成问题的一个地方是类中的字段(或更糟的是属性).例如:

One place where I think having value as a name would be really problematic would be as a field (or worse a property) in a class. For example:

public class Foo
{
    private int value;
    public int Value
    { 
        get { return value; }
        set { value = value; }    // which `value` are you setting? and to what?
    }
 }

现在您可以使用 this.value = value 消除歧义,但这仍然很丑陋,对我来说,为您的字段使用其他名称似乎更好.

Now you could remove the ambiguity here with this.value = value, but it still ugly and it seems better to me to just use a different name for you field.

这篇关于使用“值"作为C#中的标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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