alchemical
alchemical

Reputation: 13975

Basic C# property question

In C# do properties need to reference private member variables, or can I just declare the properties and use them directly in the class code?

If the former is the best practice, then does that rule out using C# property short-hand? I.e.

public string FirstName { get; set; }

Upvotes: 2

Views: 828

Answers (9)

anishMarokey
anishMarokey

Reputation: 11397

if you use like

public string FirstName { get; set; }

compiler will automatically adds getters and setters for this property automatically.it not a bad practice.

Here is the proof

alt text

if you declare

private string firstName;
        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

like this also compiler will takes it as

alt text

so its not ruled out... :)

Upvotes: 0

Jamie Keeling
Jamie Keeling

Reputation: 9966

Using properties is the best way to provide a method of control and security to the attributes in a class, always keep the attributes private if possible.

Upvotes: 0

Brian Mains
Brian Mains

Reputation: 50728

C# can reference private variables as in:

public class A
{
    private string _b;

    public string B
    {
        get { return _b; }
        set { _b = value; }
   }
}

The get;set; designation is automatic properties which when compiled will generate the private variable for you, as a way to make it easy to setup your code.

Upvotes: 0

Bala
Bala

Reputation: 256

Properties created like this

public String Caption{ get; set; } 

this will be compiled as

[CompilerGenerated]
private string <Caption>k__BackingField;

public string Caption
{
    [CompilerGenerated]
    get
    {
        return this.<Caption>k__BackingField;
    }
    [CompilerGenerated]
    set
    {
        this.<Caption>k__BackingField = value;
    }
}

The above code is extracted after compilation using reflector tool.

Upvotes: 2

JonH
JonH

Reputation: 33141

To add on to Reed's answer, inside of your code (within the class itself) the member functions should adhere to this and actually use the Property rather then the actual private member. For instance if you had this:

public string FirstName { get; set; }

And you had a strange method called public char GetFirstLetter() that returned the first letter in a person's first name you'd want to do it like this:

 public char GetFirstLetter()
        {
            return FirstName[0];
        }

Instead of actually using your private variable. When you set a property a programmer may have written code to set it in a particular manner. So it only makes sense to simply use that property within your class methods.

Upvotes: 0

Gabriel McAdams
Gabriel McAdams

Reputation: 58253

Properties do not need to reference private member variables. It is best practice, though, to have them do so. You can think of properties as methods if it makes it easier to understand. You can run code inside of them. You can return whatever you want. You can call methods and use private member variables. You can even simply return a constant.

I use private member variables in almost all cases. It allows me to create a readonly property, or to provide some rules to those outside my class of getting or setting properties that my class doesn't have to follow.

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564323

Properties, when implemented like this:

public string FirstName { get; set; }

Automatically create a private member variable (the compiler does this for you), so you don't have to worry about it. This will behave exactly the same as if you do:

private string firstName;
public string FirstName { 
    get { return firstName; }
    set { firstName = value; }
}

There is no reason not to use the automatic properties ( { get; set; } ). The provide the same advantages as making your own private member variable.

In addition, if you later decide you need to do extra processing (for example, if you decide to implement INotifyPropertyChanged in your property setter), you can add this without changing your public API, but putting a backing field in manually.

Upvotes: 27

Henk Holterman
Henk Holterman

Reputation: 273169

You don't need properties to access private fields but in general it is considered best practice.

And you can use auto-properties (short hand) untill you need to add more functionality to a property, like validation. Changing it to a 'real' property is always a non-breaking change.

Upvotes: 2

jchapa
jchapa

Reputation: 3996

They do not need to reference private member variables. You can use them directly in the class.

Upvotes: 0

Related Questions