Solved

Properties and fields

Posted on 2014-04-15
2
146 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 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