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
}
}