Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any C# naming convention for a variable used in a property?

Tags:

c#

properties

Let's say, we have a variable, which we want named Fubar

Let's say that Fubar is a String!

That means, we would define Fubar as so:

public string Fubar;

Now, let's say we want Fubar to have a getter and setter (or in other words, become a C# property)!

private string Fubar;
public string Fubar_gs
{
    get
    {
        //Some fancy logic
        return Fubar;
    }
    set
    {
        //Some more fancy logic
        Fubar = value;
    }
}

Well great! That is all fine and dandy, EXCEPT, what if I wanted the PROPERTY to be named Fubar, not the original variable?

Well obviously, I would just rename both variables. But the problem is, what would be the best name for the original variable?

Is there a naming convention for this situation?

like image 935
Georges Oates Larsen Avatar asked Aug 20 '12 21:08

Georges Oates Larsen


People also ask

Is there any C language?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C programming is an excellent language to learn to program for beginners. Our C tutorials will guide you to learn C programming one step at a time.

Is there a C+ language?

C++ is one of the world's most popular programming languages. C++ can be found in today's operating systems, Graphical User Interfaces, and embedded systems. C++ is an object-oriented programming language which gives a clear structure to programs and allows code to be reused, lowering development costs.

Is C still used in 2022?

Let's say you are new to programming. There are a variety of languages to choose from. Many people will recommend Python as your first language because of its short syntax which makes it very attractive.

Why is C called C?

After language 'B', Dennis Ritchie came up with another language which was based upon 'B'. As in alphabets B is followed by C and hence he called this language as 'C'.


4 Answers

Per Microsoft's naming conventions, the proper way would be:

private string fubar;
public string Fubar { get { return fubar; } set { fubar = value; } }

However, many people prefer to prefix the private field with an underscore to help minimize the possibility of miscapitalizing and using the field when they meant to use the property, or vice versa.

Thus, it's common to see:

private string _fubar;
public string Fubar { get { return _fubar; } set { _fubar = value; } }

The approach you take is ultimately up to you. StyleCop will enforce the former by default, whereas ReSharper will enforce the latter.

In C# 6, there is new syntax for declaring default values for properties or making read-only properties, lessening the need for properties with backing fields that don't have any special additional logic in the get and set methods. You can simply write:

public string Fubar { get; set; } = "Default Value";

or

public string Fubar { get; } = "Read-only Value";

like image 103
Daniel Mann Avatar answered Oct 16 '22 18:10

Daniel Mann


prefix the private with an underscore _fubar

as a good guide, you can use the CLR runtimes teams coding style guide which goes beyond the standard naming guideline from Microsoft

https://github.com/dotnet/runtime/blob/main/docs/coding-guidelines/coding-style.md

like image 24
Keith Nicholas Avatar answered Oct 16 '22 19:10

Keith Nicholas


If you name your private variables starting with lower case, you can right click on them and have VS generate your getter/setter code for you;

Refactor->Enacpsulate Field...

It will name the property with Caps.

like image 7
Belmiris Avatar answered Oct 16 '22 20:10

Belmiris


If there's no logic in the getter/setter, use an auto-property:

public string Fubar {get; set;}

http://msdn.microsoft.com/en-us/library/bb384054.aspx

like image 6
Austin Salonen Avatar answered Oct 16 '22 20:10

Austin Salonen