Solved

Properties and fields

Posted on 2014-04-15
2
138 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now