To underscore or to not to underscore, that is the question

Related searches

Are there any problems with not prefixing private fields with an underscore in C# if the binary version is going to be consumed by other framework languages? For example since C# is case-sensitive you can call a field "foo" and the public property "Foo" and it works fine.

Would this have any effect on a case-insensitive language such as VB.NET, will there by any CLS-compliance (or other) problems if the names are only distinguishable by casing?

It will have no effect.

Part of the recommendations for writing CLS-compliant libraries is to NOT have two public/protected entities that differ only by case e.g you should NOT have

public void foo() {...}

and

public void Foo() {...}

what you're describing isn't a problem because the private item isn't available to the user of the library

Stop Using VAR Everywhere And Think Before Using Underscore , Be a Thinker First, Then a Doer. Coverage Topic. Underscore(_) or not underscore with the private member variable; Golden Hammer - Anti-� To underscore or to not to underscore, that is the question . Posted by: admin November 21, 2017 Leave a comment. Questions:

Underscore, 2020 The Supreme Court spent 117 pages on Thursday to underscore what can be summed up in a single sentence: President Donald Trump is not above the� The underscore ( _ ) is also known as an understrike, underbar, or underline, and is a character that was originally on a typewriter keyboard and was used simply to underline words or numbers for emphasis. Today, the character is used to create visual spacing in a sequence of words where whitespace is not permitted.

Taken from the Microsoft StyleCop Help file:

TypeName: FieldNamesMustNotBeginWithUnderscore

CheckId: SA1309

Cause: A field name in C# begins with an underscore.

Rule Description:

A violation of this rule occurs when a field name begins with an underscore.

By default, StyleCop disallows the use of underscores, m_, etc., to mark local class fields, in favor of the ‘this.’ prefix. The advantage of using ‘this.’ is that it applies equally to all element types including methods, properties, etc., and not just fields, making all calls to class members instantly recognizable, regardless of which editor is being used to view the code. Another advantage is that it creates a quick, recognizable differentiation between instance members and static members, which will not be prefixed.

If the field or variable name is intended to match the name of an item associated with Win32 or COM, and thus needs to begin with an underscore, place the field or variable within a special NativeMethods class. A NativeMethods class is any class which contains a name ending in NativeMethods, and is intended as a placeholder for Win32 or COM wrappers. StyleCop will ignore this violation if the item is placed within a NativeMethods class.

A different rule description indicates that the preferred practice in addition to the above is to start private fields with lowercase letters, and public ones with uppercase letters.

Edit: As a follow up, StyleCop's project page is located here: https://github.com/DotNetAnalyzers/StyleCopAnalyzers. Reading through the help file gives a lot of insight into why they suggest various stylistic rules.

Tag merge request: [underscore] into [underscore.js], Note that there are several questions tagged underscore which do not refer to the JS library. From a quick glance, I see some referring to the character _ and� The question is whether he should use an underscore rather then an empty string between a date and someone's initials. It seems easier to scan to me, but why not just use a space and be consistent, like this: "20061223 pdb weeklytimesheet.xls" Actually what you should do is use an Access database for this stuff.

Since we are talking about a private field, it does not affect a user of your class.

But I recommend using an underscore for the private field, because it can make code easier to understand, e.g:

private int foo;
public void SetFoo(int foo)
{
  // you have to prefix the private field with "this."
  this.foo = foo;

  // imagine there's lots of code here,
  // so you can't see the method signature



  // when reading the following code, you can't be sure what foo is
  // is it a private field, or a method-argument (or a local variable)??
  if (foo == x)
  {
    ..
  }
}

In our team, we always use an underscore prefix for private fields. Thus when reading some code, I can very easily identify private fields and tell them apart from locals and arguments. In a way, the underscore can bee seen as a shorthand version of "this."

Disabling printing underscore as subscript in figures, Disabling printing underscore as subscript in. Although this allows underscores to print, it does not allow special symbols Sign in to answer this question. Main Entry: 2 underscore Function: noun 1: a line drawn under a word or line especially for emphasis or to indicate intent to italicize The term "Underscore" is usually used when referring to the line that connects names or words in an e-mail address. It may not exactly be grammatical, but many understand what it means.

After working in a environment that had very specific and very pointless style rules since then I went on to create my own style. This is one type that I've flipped back and forth on alot. I've finally decided private fields will always be _field, local variables will never have _ and will be lower case, variable names for controls will loosely follow Hungarian notation, and parameters will generally be camelCase.

I loathe the this. keyword it just adds too much code noise in my opinion. I love Resharper's remove redundant this. keyword.

6 year update: I was analyzing the internals of Dictionary<TKey,T> for a specific usage of concurrent access and misread a private field to be a local variable. Private fields definitely should not be the same naming convention as local variables. If there had been an underscore, it would have been incredibly obvious.

How to typeset an underscore character, The underscore character _ is ordinarily used in TeX to indicate a subscript in maths There is a problem, though: OT1 text fonts don't contain an underscore� I'm currently not able to type an underscore, at all or use shift to make a plus. All the other shift and key combinations work, aside from plus but that does work by hitting the plus key on the numerical part of the keyboard. I also tried entering underscore with the on screen keyboard, but that did not work either.

This has effectively broken all our mappings for all the variables that have an underscore in their names. What is worst is that we do not know if it is a permanent change or just a hiccup. The application that we are developing is a mission critical one and this sudden change in SharePoint's behavior is causing very serious concenrs about

_.contains method is used to check an element in array exists or not in Underscore.js. Prototype : _.contains(list, value, [fromIndex]) It will take three arguments first two are required and third is optional.

I did find out another way to keep the underscore. \_ does work ? I have answered my own question? Convert all underscores in the text to \_ instead of changing the interpreter to none.

Comments
  • The point of the underscore prefix, BTW, is not to deal with case issues. It's to be able to easily and visually tell fields and locals apart when reading code. I'll use it in C# and VB alike.
  • @NeilHewitt: Well, it also prevents function parameters from conflicting with member variables, which require prepending every one of them with this, which sucks. EDIT: I just responded to a four year old comment...
  • Just for clarity the official standard is _camelCase (readonly favored) github.com/dotnet/corefx/blob/master/Documentation/…
  • I prefer _camelCase for private backing stores. ie: the private filed that holds the data assigned and accessed through a property. I don't like auto properties because they can't be initialized to a known value in the class definition, and if I want a side effect in the setter, I need an explicitly declared backing store. Unfortunately, this gets refactored out when I use ^R^E in the c# editor, so I have to add it back in... every time.
  • Even though there'll be no effect, it's still a convention I'd be uncomfortable with - since it's a recipe for confusion if they differ only by case. It's just too easy to mis-read or mis-type if the only difference is the initial capital.
  • P.S. Personally, I don't underscore in C#. For me it's a personal preference, not a religious belief
  • I've done both ways and I wanted to make up my mind, one and for all, based on knowledge :P
  • I'm using underscores. It is easier to distinguish them from the arguments and local variables.
  • I use _ only for private fields, however I almost never have private fields due to 3.5 auto property. Generally the only time I have a private field is if I implement lazy loading on non-primitive types.
  • This is an amazing answer. Thanks for taking the time to compile all this information.
  • Doesn't come from C++, because in C++ reserves identifiers beginning with an underscore for language and standard library usages.
  • Why do you distinguish between performance critical, multithreaded, system level code and other code? How is using _camelCase going to help me if my code is performance critical/system level code?