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

  • Home
  • SEARCH
  • 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 3875188
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:14:17+00:00 2026-05-19T22:14:17+00:00

I am looking for a way to do this in C#: an Asker object

  • 0

I am looking for a way to do this in C#:

  1. an Asker object will ask a Giver object for Resource objects.
  2. when asked, Giver will search it’s Dictionary for existing matching Resource. If found, it will return the reference of the Resource; otherwise, it will create a the new Resource from database data, save that reference in Dictionary, and finally return the reference.
  3. the Asker may ask for the same Resource more than once, in which case Giver will return the same Resource from the Dictionary the same number of times.
  4. the Asker may at any time have no use for a given Resource, in which case, it does nothing further to the Resource.

  5. Problem: How can Giver detect for any Resource that it is no longer in use and remove it from the Dictionary? Preferably, Giver should do this without Asker’s help.

Is this possible? I can’t seem to solve this.

EDIT:
Thanks everyone for the great replies. Especially the WeakReferences. I didn’t know they were there. But I have 2 main objectives which I could have specified clearer.

  1. Giver should not rely on Asker to get notified.
  2. While a given Resource is in use, all of the references must be pointing to the same Resource so that modification to the Resource is reflected in all places where the same Resource is used.

EDIT:
[Removed incorrect code block]

  • 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-19T22:14:17+00:00Added an answer on May 19, 2026 at 10:14 pm

    To start, what you’re talking about is the basic idea behind the IDisposable interface: a deterministic way for resources to be released. While its main usage is when interacting with unmanaged resources that require explicit release (or interacting with objects that do that), its usage is not restricted to that.

    Unfortunately, it fails your last requirement: since it’s deterministic, it has to be called by somebody. This somebody would have to be the Asker.

    The only solution that I can come up with would be using the WeakReference class in your Giver object. This allows you to maintain a reference to an instance that doesn’t prevent it from being garbage collected (after it’s collected, your reference becomes null).

    Unfortunately, this isn’t deterministic. Your reference will become null (and IsAlive will be false) after the object is actually collected, which is not guaranteed to happen at any particular time (or at all during the lifetime of your application).

    With those caveats in mind, you could something like this:

    public class Giver
    { 
        private Dictionary<string, WeakReference> cache = 
            new Dictionary<string, WeakReference>();
    
        public object GetResource(string resourceName)
        {
            WeakReference output;
            object returnValue = null;
    
            if(cache.TryGetValue(resourceName, out output))   
            {
                if(output.IsAlive) returnValue = output.Target;
    
                if(returnValue == null) cache.Remove(resourceName);
            }
    
            if(returnValue == null)
            {
                returnValue = ...; // get the actual resource
    
                cache.Add(resourceName, new WeakReference(returnValue));
            }
    
            return returnValue;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I guess this question was asked in one way or another, but I'm looking
This might have been asked before but I'm really looking for a simple way
I'm looking for a way to implement this design in wxPython on Linux... I
Arising out of this question, I'm looking for an elegant (ruby) way to compute
I looking for a way, specifically in PHP that I will be guaranteed to
Pardon me if this question has already been asked. HttpContext.Current.Session["key"] returns an object and
I know this question has been asked a bit before. But looking around I
I'm looking for way to PHP to detect if a script was run from
I am using the jquery-ui-dialog plugin I am looking for way to refresh the
I looking for a way to programmatically start a VOIP call using the SIP

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.