0

I recently saw a piece of C# code that utilized the ref keyword for Dictionary parameter in a method (example below). When I came across this, I really couldn't understand why someone would use a ref here. A C# Dictionary is a reference type. There shouldn't be any need for one to pass it as a reference.

I'd like to think I'm right here, but this piece of code came from a source that I thought was reputable. For reference, the lines I'm referring to are in the getMiddlePoint method in the 'C# - IcoSphere' section.

Now on top of this being straight from a wiki.unity3d page, I also found it being adjusted and repeated in several other places. Probably a copy and paste issue but still. Am I missing something?

Is there a reason to use the ref keyword for a C# Dictionary parameter?

public static class WhyTheRef
{
    public static void MainMethod()
    {
        Dictionary<long, int> testDictionary = new Dictionary<long, int>();

        WeirdParamMethod(ref testDictionary);
    }

    public static void WeirdParamMethod(ref Dictionary<long, int> dictionaryParam)
    {
        // do something with dictionaryParam
    }
}
Jee
  • 969
  • 1
  • 7
  • 26
  • 2
    If `WeirdParamMethod` stores a new instance of `Dictionary` into `dictionaryParam`, there is a reason for `ref`, otherwise there isn't. – GSerg Nov 13 '20 at 08:57
  • as you don´t provide what `WeirdParamMethod` does, there´s no way for us to determine if or if not the keyword is needed here. As long as you only **modifiy** an existing object, there´s no need for ref. If you want to pass a **new instance**, however... – MakePeaceGreatAgain Nov 13 '20 at 08:57
  • 1
    See the linked question, but the quick answer is: you can change the _contents_ of the dictionary that `dictionaryParam` points to without `ref`. With `ref`, you can replace the object that `testDictionary` in the `MainMethod` points to form within the `WeirdParamMethod` by assigning a new value to `dictionaryParam`. – ProgrammingLlama Nov 13 '20 at 08:58
  • @John Yeah I understand your point and the linked question also helps explain it some more too. I guess I'm just having a hard time wrapping my head around the fact that without `ref`, you can't make the original object point to something completely new from within `WeirdParamMethod`. I believe it, but am definitely having a bit of trouble visualizing it. – Jee Nov 13 '20 at 09:23
  • Arguments are passed to methods by value by default (without ref). That means a copy is made and method operates on that copy. So without ref, `testDictionary` and `dictionaryParam` are not the same. They both contain the same reference (same address basically, pointing to dictionary contents), but they are different (have different locations in memory). So assigning something to `dictionaryParam` won't change `testDictionary `. Using `ref` changes that to pass by reference - now `testDictionary` and `dictionaryParam` are identical (have the same location in memory). – Evk Nov 13 '20 at 09:32
  • Think of it like this: `ref` isn't about how you pass your dictionary object. It's about how you pass the object (the variable) that holds a reference to it. Without `ref`, you pass a copy of the variable. With `ref`, you pass the variable itself. In terms of memory, imagine your dictionary is C, and `testDictionary` is B. The memory that stores the existence of that variable is `A`. So we have the situation that A points to B, and B points to C. If you don't have `ref`, when you pass `testDictionary` to `WeirdParamMethod`, you make a copy of B (B2) which also points to C. – ProgrammingLlama Nov 13 '20 at 09:32
  • B2 is the method's alone to use and do what it likes with. If you do have `ref`, you actually pass a copy of A into the method, meaning that you can update the value that B holds (it's not a copy as with B2, it's actually the same piece of memory as B in `MainMethod`). So if you change B to point at a new value of C (C2), then that is reflected in `testDictionary`. – ProgrammingLlama Nov 13 '20 at 09:33

0 Answers0