Solved

Properties and fields

Posted on 2014-04-15
2
145 Views
Last Modified: 2014-04-16
I know this is a basic question but I was watching PluralSight and was speaking with a coworker.

I've attached a screenshot of the lesson.  It doesn't need the
private int _age
public int Age
  {...}

Don't think it does because we just use {get; set;}  That's what I'm trying to explain to my coworker.
properties.png
0
Comment
Question by:Camillia
2 Comments
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 250 total points
ID: 40002572
You are correct.

The "backing field" _age is needed only if you want to reference it in some way, like the traditional:

private int _age
public int Age
{
     get { return _age;}
    set {
              _age = value;
// and maybe some more processing involving _age here....

}
}

Open in new window

0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 250 total points
ID: 40002766
What you are referring to are known as "Automatic Properties" which declare and manage the hidden member value for you. It is basically a shorthand to save you having to write boiler plate code for properties that do nothing more than read/write directly to a value.

If you need to build more logic into your property then you need to use the long hand version.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
edit .asp files 5 48
asp.net figure out error 3 25
Problem to copy file 14 52
c#, datatable, aspx 4 22
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall‚Ķ
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question