Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8792159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:56:28+00:00 2026-06-13T22:56:28+00:00

I have one c# function which returns me List of States. I want this

  • 0

I have one c# function which returns me List of States. I want this function should get called only once like static variable.




    public List GetStateList()
    {
       List lstState=new List();
       lstState.add("State1");
       lstState.add("State2");
       lstState.add("State3");

       return lstState;
    }

I m calling this function from many places since this state list is going to be same so i want this function should get called only once, and next time when this function is getting called it should not re create the whole list again.

How could i achieve this in c#.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:56:29+00:00Added an answer on June 13, 2026 at 10:56 pm

    Memoise it. It’ll still be called multiple times, but only do the full work once:

    private List<string> _states; //if GetStateList() doesn't depend on object
                                  //state, then this can be static.
    public List GetStateList()
    {
       if(_states == null)
       {
         List lstState=new List();
         lstState.add("State1");
         lstState.add("State2");
         lstState.add("State3");
         _states = lstState;
       }
       return _states;
    }
    

    Depending on threading issues, you may wish to either:

    1. Lock on the whole thing. Guaranteed single execution.
    2. Lock on the assignment to _states. There may be some needless work in the early period, but all callers will receive the same object.
    3. Allow for early callers to overwrite each other.

    While the last may seem the most wasteful, it can be the best in the long run, as after the initial period where different calls may needlessly overwrite each other, it becomes a simpler and faster from that point on. It really depends on just how much work is done, and how often it may be concurrently called prior to _states being assigned to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQL function which returns a list of Teams. I want to
I have a function which returns a one-sided intersection of values between two input
Using xlwDotNet, I have created a simple function which returns one number. Sometimes, something
I have a function which takes in two parameters, and returns one or the
I have one constructor function, which acts as a superclass: Bla = function(a){this.a =
I have one RadComboBox. I fill it with a function which returns for example
I have a function that returns a list via yield. I use this function
I have made one function in which I am passing a matrix and returning
I have a function which is described in one of my controllers. The function
I have a PL/pgSQL function which requires one input parameter which is the primary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.