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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:28:27+00:00 2026-05-16T04:28:27+00:00

One common pattern I see and use frequently in C++ is to temporarily set

  • 0

One common pattern I see and use frequently in C++ is to temporarily set a variable to a new value, and then reset it when I exit that scope. In C++, this is easily accomplished with references and templated scope classes, and allows for increased safety and prevention of errors where the variable is set to a new value, then reset to an incorrect assumed initial value.

Here is a simplified example of what I mean (in C++):

void DoSomething()
{
    // The following line captures GBL.counter by reference, stores its current
    // value, and sets it to 1
    ScopedReset<int> resetter(GBL.counter, 1);

    // In this function and all below, GBL.counter will be 1
    CallSomethingThatNeedsCounterOf1();

    // When I hit the close brace, ~ScopedReset will be called, and it will
    // reset GBL.counter to it's previous value
}

Is there any way to do this in C#? I’ve found the hard way that I can’t capture a ref parameter inside an IEnumerator or a lambda, which were my first two thoughts. I don’t want to use the unsafe keyword if possible.

  • 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-16T04:28:28+00:00Added an answer on May 16, 2026 at 4:28 am

    The first challenge to doing this in C# is dealing with non-deterministic destruction. Since C# doesn’t have destructors you need a mechanism to control scope in order to execute the reset. IDisposable helps there and the using statement will mimic C++ deterministic destruction semantics.

    The second is getting at the value you want to reset without using pointers. Lambdas and delegates can do that.

    class Program
    {
        class ScopedReset<T> : IDisposable
        {
            T originalValue = default(T);
            Action<T> _setter;
            public ScopedReset(Func<T> getter, Action<T> setter, T v)
            {
                originalValue = getter();
                setter(v);
                _setter = setter;
            }
    
            public void Dispose()
            {
                _setter(originalValue);
            }
        }
    
        static int counter = 0;
    
        static void Main(string[] args)
        {
            counter++;
            counter++;
    
            Console.WriteLine(counter); 
            using (new ScopedReset<int>(() => counter, i => counter = i, 1))            
                Console.WriteLine(counter);
    
            Console.WriteLine(counter);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How should one have several different controller' actions set a common instance variable for
One common pattern when caching with Django is to use the current site's ID
So one of the common tasks that I do as a programmer is debugging
I have one variable var1='common_location/specific_location_1/common_filename_text' now I have to get a new variable var2='common_location/specific_location_2/common_filename_text'.
My problem is one that you would think is quite common, but I haven't
I want to group the common methods in one file and use it. For
It is a common pattern I see where the error codes associated with an
Does anyone know of papers/books/etc. that document patterns for databases? For example, one common
I am moving a system from a VB/Access app to SQL server. One common
I have 2 table t1 -> t2 (common one to many relationship) with 140.000

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.