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 6799059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:48:12+00:00 2026-05-26T18:48:12+00:00

For some static methods I realise it is extremely convenient to use a small

  • 0

For some static methods I realise it is extremely convenient to use a small array to temporarily store values during an operation. Said array is useful because you need indexing, but allocating that small array everytime the method is invoked.

Is this a good way to work around the lack of C-like static locals in C#?

[ThreadStatic]private static int[] staticregister = new int[4];

public static bool CoolStaticMethod(int[] largearray)
{
    //...
}

My assumption is that a method which can’t call itself, either directly (recursive) or indirectly, can only be called singularly in a single thread, thus the fake static local should be declared thread-static and the problem is largerly solved.

Edit:

I must add that the contents of the register is garbage between method invocations.

  • 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-05-26T18:48:12+00:00Added an answer on May 26, 2026 at 6:48 pm

    It is not what I would call a good workaround, no. It will work (assuming you are sure about the re-entrancy risks, i.e. not calling into itself, even via accidental events/callbacks/etc) – but…

    In my opinion, it is stateful, make it an instance:

    private int[] register = new int[4];
    public bool CoolMethod(int[] largearray) {...}
    

    and simply use different instance of WheverTheTypeIs for each context, i.e. the instance acts as the context. Just use a different instance per thread if you want context per-thread. This also allows usage with callbacks, parallelism, workers, etc to continue in the same context. Note that there are many frameworks that do not guarantee a single thread (WCF, ASP.NET, WPF for examples), and this is only going to increase as 5.0 introduces more async/await-oriented code.

    If you are deeply tied to static methods, passing the register in as a second parameter would suffice too:

    public static bool CoolStaticMethod(int[] largearray, int[] register) {...}
    

    If the issue is the allocation of a 4-byte array:

    1. that will usually be GEN-0, so cheap to collect
    2. if you really want, use stackalloc and unsafe to avoid the allocation

    For an example of “2”:

    public static unsafe bool CoolStaticMethod(int[] largearray)
    {
        // not an array! this is raw data on the stack; DO NOT GO OUT OF BOUNDS!
        int* register = stackalloc int[4]; 
    
        register[0] = 1;
        register[1] = largearray[3];
        largearray[2] = register[0];
        ....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a utility class with some static methods which I use in some
I'm trying to convert an old 'C' program containing some static methods into Obj-c
I have a class that must have some static methods. Inside these static methods
My singleton accessor method is usually some variant of: static MyClass *gInstance = NULL;
I have some static images in a folder on my IIS 6-based website that
I have some static resources (images and HTML files) that will be localized. One
How can I serve some static content in liferay? I have a directory structure
I have some static text that needs to show up at 2 locations within
What is the right way to perform some static finallization? There is no static
I'm using HAML to generate some static html pages for a site, and I

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.