Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "value" as an identifier in C#

Tags:

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.

like image 408
Matt Martin Avatar asked Jul 13 '15 19:07

Matt Martin


People also ask

How do you declare an identifier in C?

You create an identifier by specifying it in the declaration of a variable, type, or function. In this example, result is an identifier for an integer variable, and main and printf are identifier names for functions. Once declared, you can use the identifier in later program statements to refer to the associated value.

What is an identifier variable in C?

C Identifiers Identifier refers to name given to entities such as variables, functions, structures etc. Identifiers must be unique. They are created to give a unique name to an entity to identify it during the execution of the program.

Can a variable be an identifier?

Variable is only a kind of identifier, other kinds of identifiers are function names, class names, structure names, etc. So it can be said that all variables are identifiers whereas, vice versa is not true.

What is identifier example in C?

C identifiers represent the name in the C program, for example, variables, functions, arrays, structures, unions, labels, etc. An identifier can be composed of letters such as uppercase, lowercase letters, underscore, digits, but the starting letter should be either an alphabet or an underscore.


1 Answers

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.

like image 72
Matt Burland Avatar answered Sep 18 '22 05:09

Matt Burland