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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:23:02+00:00 2026-05-29T05:23:02+00:00

Often I need to minimise object allocations within code that runs very frequently. Of

  • 0

Often I need to minimise object allocations within code that runs very frequently.
Of course I can use normal techniques like object pooling, but sometimes I just want something that’s contained locally.
To try and achieve this, I came up with the below:

public static class Reusable<T> where T : new()
{
    private static T _Internal;
    private static Action<T> _ResetAction;

    static Reusable()
    {
        _Internal = Activator.CreateInstance<T>();
    }

    public static void SetResetAction(Action<T> resetAction)
    {
        _ResetAction = resetAction;
    }

    public static T Get()
    {
#if DEBUG
        if (_ResetAction == null)
        {
            throw new InvalidOperationException("You must set the reset action first");
        }
#endif

        _ResetAction(_Internal);

        return _Internal;
    }
}

Currently, the usage would be:

// In initialisation function somewhere
Reuseable<List<int>>.SetResetAction((l) => l.Clear());

....

// In loop
var list = Reuseable<List<int>>.Get();
// Do stuff with list

What I’d like to improve, is the fact that the whole thing is not contained in one place (the .SetResetAction is separate to where it’s actually used).

I’d like to get the code to something like below:

// In loop

var list = Reuseable<List<int>>.Get((l) => l.Clear());
// Do stuff with list

The problem with this is that i get an object allocation (it creates an Action<T>) every loop.

Is it possible to get the usage I’m after without any object allocations?

Obviously I could create a ReuseableList<T> which would have a built-in Action but I want to allow for other cases where the action could vary.

  • 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-29T05:23:03+00:00Added an answer on May 29, 2026 at 5:23 am

    Are you sure that creates a new Action<T> on each iteration? I suspect it actually doesn’t, given that it doesn’t capture any variables. I suspect if you look at the IL generated by the C# compiler, it will cache the delegate.

    Of course, that’s implementation-specific…

    EDIT: (I was just leaving before I had time to write any more…)

    As Eric points out in the comment, it’s not a great idea to rely on this. It’s not guaranteed, and it’s easy to accidentally break it even when you don’t change compiler.

    Even the design of this looks worrying (thread safety?) but if you must do it, I’d probably turn it from a static class into a “normal” class which takes the reset method (and possibly the instance) in a constructor. That’s a more flexible, readable and testable approach IMO.

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

Sidebar

Related Questions

I use emacs for development and very often need to move to the start
I often need to design a dialog in Delphi/C++Builder that allows various properties of
I often need to use a function which performs and action X is condition
I have two scripts that often need to be run with the same parameter:
I Often need UUIDs inserted into files that I'm editing in Notepad++. Does anyone
I very often need to do some Emacs magic on some files and I
I use Visual Studio 2010 and ReSharper 6.1. I often need to create some
The Problem : Object models built using an ORM often need to perform multiple
I often need to match a tuple of values that should have the same
When we do development/QA we often need to pretend that current date/time is something

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.