Solved

getter and setter for List<  >?

Posted on 2013-06-28
4
730 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:knowlton
  • 2
  • 2
4 Comments
 
LVL 74

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: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 74

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:knowlton
ID: 39285838
Okay - I understand better now.

Wow - that is kinda cool.  : )

I'll have to remember this!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

759 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

22 Experts available now in Live!

Get 1:1 Help Now