0

I am somewhat new to C# and just wondering if it is possible to get a reference to a value in a Dictionary.

I am currently working on an application that uses a singleton design with the idea that I can have access to all objects, and ideally be able to get a reference to the requested value stored in the Dictionary to have direct access to the objects outside the singleton class.

I know you can get the value with dictionary_.ElementAt(id) is there a way to get the reference to the element value?

Zazza
  • 11
  • 4
  • 3
    Unless the item type stored in the dictionary is a value type, a reference is what you're already getting when accessing the contents of the dictionary. – Mathias R. Jessen Sep 24 '21 at 14:43
  • does this solve your problem? https://stackoverflow.com/questions/24301953/get-value-from-dictionary-as-reference – MistaGoustan Sep 24 '21 at 14:44
  • 4
    [`dictionary_.ElementAt(id)`](https://stackoverflow.com/a/2254480/11683) has to be the worst way of accessing elements in a dictionary. It defies the very purpose of the dictionary by throwing away its lookup capabilities and indexing into it like an array, in the slowest way possible, by enumerating all elements up to `id`, all while confusing the future readers of the code by tricking them into thinking that the `id` is the id, not the numerical index. – GSerg Sep 24 '21 at 14:45
  • no the idea is to get the reference of the value in the dictionary to send it to other classes to directly apply changes to the value stored in the dictionary. outside the declaration space of the dictionary itself – Zazza Sep 24 '21 at 14:50
  • 1
    @Zander_Wessels You appear to have your C++ glasses on, they are unhelpful in C#. Please re-read [the top comment](https://stackoverflow.com/questions/69316886/get-reference-from-dictionary-value-c-sharp#comment122515821_69316886), then possibly also https://stackoverflow.com/q/5057267/11683. – GSerg Sep 24 '21 at 14:52
  • 2
    If you're storing non value types in the dictionary, then what comes out of the dictionary is a reference to the one same object already stored in the dictionary. Any changes to that *one* object will be visible either from the new reference or by relocating it via the dictionary. – Damien_The_Unbeliever Sep 24 '21 at 14:52
  • 1
    Note that `ElementAt` is not even a method of the dictionary, it is a LINQ extension method. Use `dict.TryGetValue(key, out var value)` instead or `dict[key]`. If the element type is a class, then you will automatically get a reference and you can change any property of it, except properties used in the dictionary key, because that would mess up the dictionary. – Olivier Jacot-Descombes Sep 24 '21 at 14:54
  • @GSerg okay yeah that's my mistake thanks – Zazza Sep 24 '21 at 14:55

1 Answers1

1

First, there are some ways to get a value from a dictionary (I will show two ways).

  1. dict.TryGetValue(key, out var value)
  2. dict[key]

if the object inside dict[key] is a Class instance, you will get a reference to it. if the object inside dict[key] is a value (int, double, boolean... those are called struct in C#), you will get the value, and not a reference to it.

What I suggest you is to think as the 'dictionary' and the 'key' as your reference.

But, you can create a new class, and make it store your object - its NOT a great solution, and I do not recommend you to use it, but keep it in mind.

class Wrapper<T>
{
    public T Data {get;set;}
}

and then your dictionary will be like that: dictionary<type_of_your_choice, Data<type_of_your_choice>>.

Then dictionary[key] will return an instance reference, I think the next steps are clear.

Abra Cadabra
  • 141
  • 6