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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:07:14+00:00 2026-06-05T18:07:14+00:00

I want to create the dictionary of all the ViewModels. public static Dictionary<string, WeakReference>

  • 0

I want to create the dictionary of all the ViewModels.

   public static Dictionary<string, WeakReference> vmCollection = new Dictionary<string, WeakReference>();

Adding it like this

 vmCollection.Add(name, new WeakReference(viewModel));

And calling the required method like this..

((vmCollection[viewModel].Target) as BaseViewModel).NewMessage(message);

Do I need maintain it as a WeakReference? What could be the consequences if I don’t maintain it as a WeakReference.

  • 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-05T18:07:16+00:00Added an answer on June 5, 2026 at 6:07 pm

    The only consequence of not using a WeakReference is that the reference in your dictionary will prevent the View Model instances from being garbage collected. A WeakReference allows garbage collection (assuming there are no other solid references elsewhere).

    An item becomes eligible for garbage collection when it has no references to it. WeakReference does not create a “countable” reference, thus you can keep a sort-of-reference to it, but still let it be eligible if your WeakReference is the only thing left looking at it.

    Whether you need it or not really depends on what sort of life-cycle your View Models have. If they need disposing or otherwise “letting go of”, then you may need to use WeakReference or expose a way to remove the reference from the dictionary instead.

    As I mention in the comments. I tend to err away from using WeakReference as opposed to handling the life-cycle of the relevant objects explicitly. That said, they are useful when you simply don’t have visibility of the life-cycle at the relevant points. I think in your situation, you should have the necessary visibility, as these are all likely in the UI layer, and thus should try to not use them.

    Here is a resource on the topic:

    • Weak References MSDN article

    Guidelines extract from the above MSDN link:

    Use long weak references only when necessary as the state of the
    object is unpredictable after finalization.

    Avoid using weak references to small objects because the pointer
    itself may be as large or larger.

    Avoid using weak references as an automatic solution to memory
    management problems. Instead, develop an effective caching policy for
    handling your application’s objects.

    I believe the last guideline point applies to your situation.

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

Sidebar

Related Questions

I have a dictionary in python like this: x = {country:{city:population}:......} and I want
I want to create a Dictionary<DateTime, Dictionary<APerson, AVisit>> struct and I want to supply
Suppose I want to create a dictionary that maps digits to numbers less than
I want to create a Python dictionary which holds values in a multidimensional array
I have C# Dictionary and I want create a .csv file from it. For
I want create a new JavaScript object based on an existing JavaScript object. Till
I have an NSArray with several NSDictionaries . I want to create a new
In Python, I want to create a new object by loading a number of
In my class, I want to use a dictionary with the following declaration: Dictionary<string,
This is a homework assignment, just for all that want to know. I'm writing

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.