Solved

getter and setter for List<  >?

Posted on 2013-06-28
4
777 Views
Last Modified: 2013-06-28
I am not familiar with this syntax.

What is happening here with the getter and setter just sort of tacked on the end?

protected List<MerchantStates> merchantStates { get; set; }

Open in new window

0
Comment
Question by:Tom Knowlton
[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
  • 2
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39285817
It's an "automatic" property. It saves you from having to do this:

private List<MerchantStates> _merchStates;

protected List<MerchantStates> merchantStates
{
    get { return this._merchStates; }
    set { this._merchStates = value; }
}

Open in new window

0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 39285826
I see:

In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects. When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.



But List<  >   already has Add(   )

Why would you need a setter?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39285831
You're not modifying List<>; you're having the compiler create the equivalent of what I displayed above. If you do not use the { get; set; } syntax, then you have to do what I demonstrated in the snippet above. That is, you have to create a backing field (i.e. private member variable) that the property makes reference to. In this case, the backing field just happens to be of type List<MerchantStates>.

Because we've declared both a get and set, we can now do:

classInstance.merchantStates  = new List<MerchantStates>();

Open in new window


The fact that we used "{ get; set; }" doesn't affect this--we could have used the expanded version I demonstrated above.
0
 
LVL 5

Author Closing Comment

by:Tom Knowlton
ID: 39285838
Okay - I understand better now.

Wow - that is kinda cool.  : )

I'll have to remember this!
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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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