Solved

getter and setter for List<  >?

Posted on 2013-06-28
4
758 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 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: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:knowlton
ID: 39285838
Okay - I understand better now.

Wow - that is kinda cool.  : )

I'll have to remember this!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 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