Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

263
Vistas
Using "value" as an identifier in C#

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...
}

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

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

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?

I was surprised that I wasn't able to find this question already asked on SO, and I suspect that someone has asked it before. However, it is difficult to search for because so many posts have "variable" and "identifier" in the title. I was unable to find information about this on MSDN.

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

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

In a property setter, the variable name value is reserved. It is used as the name of the variable which can be assigned to a backing field.

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 considered bad practice?

It's only reserved in the property setter. It's a very generic name, but it can often be the best description of the variable you are working with.

MSDN info

over 4 years ago · Santiago Trujillo Denunciar

0

Here's what MSDN says:

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.

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; }
    }
}

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

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.

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

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.

From MSDN:

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

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.

Edit: 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?
    }
 }

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.

over 4 years ago · Santiago Trujillo Denunciar

0

It is fine to use value as an identifier anywhere outside a set accessor. The C# Language Specification (linking old version) says:

Since a set accessor implicitly has a parameter named value, it is a compile-time error for a local variable or constant declaration in a set accessor to have that name.

The Word value is not (and was never) a full keyword in C#, even if it has had this special use in setters ever since C# 1.

See value (C# Reference) for more.

Of course, if you have a field (class-level variable) called value and you want to access it from within a set accessor, use this.value (or NameOfYourType.value for static fields).


For a list of real keywords and contextual "keywords", also see C# Keywords.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda