Are "=>" Expressions Called Right Away?

are part of speech
are truck caps
area
are synonym
are land measure
are tonneau cover
are truck caps models
are dealers

I have numerous lists of type List<SelectListItem> in my model that are set using the => Expression like so:

public List<SelectListItem> Users => Service.GetAll<User>().ToSelectList();

I was under the impression that these were called when the class was created so I moved them into a LoadLists methods so that I could call the specific method when needed for efficiency.

public List<SelectListItem> Users {get;set;}

public void LoadLists()
{
   this.Users = Service.GetAll<User>().ToSelectList();
}

This caused an issue on my views as the Users select list was being used in multiple DropdownListFors that caused any nullable DropdownListFor to hold the most recent value that was using the Users select list.

When using the original public List<SelectListItem> Users => Service.GetAll<User>().ToSelectList(); this issue didnt occur as Im assuming it was called each time the Users select list was being used on the view?

So I created a dummy List<SelectListItem> like:

public List<SelectListItem> Test => Service.GetAll<User>().ToSelectList();

and didn't use it on the view and the debug line was never hit. So my question is is => Expressions only executed when they are used somewhere?

This code:

public List<SelectListItem> Users => Service.GetAll<User>().ToSelectList();

defines a property with only a get accessor. It means exactly the same as this:

public List<SelectListItem> Users
{
    get { return Service.GetAll<User>().ToSelectList(); }
}

The => notation was introduced as a syntax alternative for doing the same with fewer lines of code.

And to answer your question: yes, every time Users is called, the property body executes, for both notation variants.

Are, Are definition, present indicative plural and 2nd person singular of be. See more. A.R.E. Truck Caps & Truck Accessories manufacturer of fiberglass pick-up truck caps, truck canopies, tops, toppers, truck toppers, camper shells, canopies, hard tonneau covers, work caps and truck accessories.

public List<SelectListItem> Users => Service.GetAll<User>().ToSelectList();

Is just syntactic sugar for:

public List<SelectListItem> Users { 
    get { 
        return Service.GetAll<User>().ToSelectList();
    }
}

It is called an expression-bodied property.

Are, Are definition is - present tense second-person singular and present tense plural of be. Are definition, present indicative plural and 2nd person singular of be. See more.

=> can mean different things in different context. In this scenario, it simply denotes a way of describing a property getter. Property getters are not invoked "immediately" - rather, they are invoked only when the property is accessed, and every time the property is accessed.

The "immediate" version (at object initialization time) would be:

public List<SelectListItem> Users {get;} = Service.GetAll<User>().ToSelectList();

which is an automatically implemented property backed by a field with a field-initializer that is run before the type's constructor.

ARE, Are is the second-person singular and plural forms of the verb "to be", the copula of the English language. Are, ARE or Åre may also refer to:  are 1 (är) v. Second person singular and plural and first and third person plural present indicative of be. [Middle English aren, from Old English aron, earon; see er- in

are, The pronunciation /aʊɚ/ arising from confusion of "are" and "our" is rare, however it results as the latter can be elided into /ɑɹ/ in quick speech. TranslationsEdit. Are, basic unit of area in the metric system, equal to 100 square metres and the equivalent of 0.0247 acre. Its multiple, the hectare (equal to 100 ares), is the principal unit of land measurement for most of the

Pools in the Mexican desert are a window into Earth's early life , Among the most recent additions to that menagerie are hundreds of species of archaea, the ancient microbes that may have given rise to  Alexandria Real Estate Equities (ARE) has been upgraded to a Zacks Rank 2 (Buy), reflecting growing optimism about the company's earnings prospects. This might drive the stock higher in the near

Republican Leaders Want to End Obamacare. Their Voters Are , Their Voters Are Expanding It. Oklahoma is the latest state where voters, in choosing to expand Medicaid, have delivered a rebuke to their elected  When deciding whether to use is or are, look at whether the noun is plural or singular. If the noun is singular, use is. If it is plural or there is more than one noun, use are. The cat is eating all of his food.

Comments
  • It's just syntactic sugar - under the hood, it's exactly like you wrote the method longhand.
  • "I was under the impression that these were called when the class was created" Then your impression is whrong. => in this context is just a simplification for a normal public List<SelectListItem> Users get { return Service.GetAll<User>().ToSelectList(); } which is executed when you access that property.
  • Say I had 3 DropdownListFors that used the User list. For each of the 3 DropdownListFors the debugger goes an hits that expression which means the select lists are their own. When I put it in the method its only called once so each DropdownListFor has reference to the same user list which maintains the Selected value.
  • "So my question is is => Expressions only executed when they are used somewhere?" Why not just try it out by putting a breakpoint and see, when it´s hit?
  • I did. I said what I found in my above comment and in the OP with the Test select list. So I'm assuming its only ever called when the property is access and can be called multiple times depending on how many times the property is reference?
  • "So my question is is => Expressions only executed when they are used somewhere?"
  • So its only called if the property is accessed?
  • Yes, Service.GetAll<User>().ToSelectList(); is called when the property is accessed.
  • @BadDub Yes. And note: It is called each time the Property is accessed. So you'll have a different list ( instance ) every time.