I have seen variables like _ image and was wondering what _ meant?
12 Answers
It doesn't mean anything. It is rather a common naming convention for private member variables to keep them separated from methods and public properties. For example:
class Foo
{
private int _counter;
public int GetCounter()
{
return _counter;
}
public int SetCounter(int counter)
{
_counter = counter;
}
}

- 362,284
- 104
- 897
- 1,065

- 8,523
- 5
- 32
- 48
-
2No, it isn't convention. In C# the _ variable style is a legacy thing _(can't exactly remember where it came from, some old reference book do mention.)_ It is even use in view like _ abc.cshtml, but the thing is, there was some issue with old framework / system, nowadays the newer dot Net doesn't require any stupid thing like that, it is bring forward from legacy broken stuff. The only use case for _variable is when you want to avoid name collision between the C# get set accessor with your private member. But still, you rarely use that unless your get set accessor has validation logic. – A. Go Dec 09 '20 at 04:30
In most languages _
is the only character allowed in variable names besides letters and numbers. Here are some common use cases:
- Separating words:
some_variable
- Private variables start with underscores:
_private
- Adding at the end to distinguish from a built-in name:
filter_
(sincefilter
is a built-in function) - By itself as an unused variable during looping:
[0 for _ in range(n)]
Note that some people really don't like that last use case.

- 202,379
- 35
- 273
- 306
-
1I've seen many instances where `_varname` indicates a private variable. – eykanal Aug 01 '11 at 19:51
Some people use it to indicate that they are variables rather than (say) method names. Or to make it obvious that they're instance variables rather than local variables. Sometimes you see extra prefixes, e.g.
private int m_age; // Member (instance) variable
private static int g_maxAge; // Global (static) variable
It's just a convention. I was going to say "there's nothing magic about _" but that's not quite true - in some languages a double underscore is reserved for "special" uses. (The exact usage depends on the language of course.)
EDIT: Example of the double underscore rule as it applies to C#. From the C# 4 spec, section 2.4.2:
Identifiers containing two consecutive underscore characters (U+005F) are reserved for use by the implementation. For example, an implementation might provide extended keywords that begin with two underscores.

- 1,421,763
- 867
- 9,128
- 9,194
-
By "some languages" you mean Python right? Or are there other languages that do that as well? – Davy8 Aug 01 '11 at 19:10
-
@Davy8: Actually I was thinking of C, C++ and C#. (I'm not sure about the first two, but I'm reasonably confident. I'm sure about C#.) – Jon Skeet Aug 01 '11 at 19:19
-
In C and C++, one leading underscore + capital letter is also reserved. Use of leading underscores in user code is discouraged. (In general, having two identical-except-for-underscore names is discouraged, because the two are easily confused.) – Ben Voigt Aug 01 '11 at 19:23
The underscore in variable names is completely optional. Many programmers use it to differentiate private variables - so instance variables will typically have an underscore prepended to the name. This prevents confusion with local variables.

- 626
- 3
- 9
_
usually means something private or internal. In C++ standard libraries all implementation specific variables must start with _
.

- 30,896
- 18
- 85
- 139
The use of two underscores (`__') in identifiers is reserved for the compiler's internal use according to the ANSI-C standard.
Underscores (`_') are often used in names of library functions (such as "_main" and "_exit"). In order to avoid collisions, do not begin an identifier with an underscore.

- 11
- 1
Usually it separates class fields from the variables. To avoid using this
in code constructions.
class MyClass {
private int _myIntField;
private void setMyIntField(int value2Set) {
_myIntField = value2Set;
}
}
-
Nope, it doesn't need *this* . to know it is private variable. It was legacy thing bring into C# but modern dot Net doesn't enforce nor require those nonsense. It is legacy broken thing, and it is another way for people to write ugly code with unsound justification for it. (It may be useful in some case, but rarely, and please stop that.) Please pardon young programmers from subsequent generations. – A. Go Dec 09 '20 at 04:37
Another use case (mainly in javascript) is when you need to assign the current instance this to a local variable we write as below
var _this = this;
If you need to create a local temporary object reference, to differentiate between the actual needed reference, we create as below
List<Employee> employeeList = new ArrayList<>();
for (Employee _employee : employeeList) {}
So if we follow this best practice, every time you see a variable with _ , we come to a conclusion that its being used to solve some business need at that particular method.

- 1,512
- 13
- 11
To avoid reserved keywords, or in reserved keywords, making them more easily avoided. A single underscore is discouraged and reserved in JavaSE9.
Basically it is telling that the developer should provide the definition . In short it defines it does not have any definition .

- 1
- 1
In most languages, it doesn't actually affect the functionality of the code, but is often used to denote reserved or internal names.
It is common in some languages to name your instance variable _image
or image_
and then make the public method used to access it image()
.
Similarly, some names like __FILE__
are used in some languages to denote a special variable or constant created by the interpreter or compiler; such names are often reserved to encourage programmers to avoid using those names in their own programs in case the names are used in future versions of the language.

- 16,137
- 1
- 43
- 44