C
C#9mo ago
fifty Shucks

C# Dictionary question

Hi, if i have a dictionary in C# defined like so: Dictionary<string, List<int>> test; and I do List<int> stringValue = test["string"]; is the resulting variable "stringValue" a copy of test["string"] or the actual list ???
3 Replies
Angius
Angius9mo ago
A list is a reference type, so a reference to that list We can easily check that, though
MODiX
MODiX9mo ago
Angius
REPL Result: Success
var dict = new Dictionary<string, List<int>>(){
["key"] = [ 1, 2, 3, 4, 5 ]
};

var list = dict["key"];
list.Add(17);

dict["key"]
var dict = new Dictionary<string, List<int>>(){
["key"] = [ 1, 2, 3, 4, 5 ]
};

var list = dict["key"];
list.Add(17);

dict["key"]
Result: List<int>
[
1,
2,
3,
4,
5,
17
]
[
1,
2,
3,
4,
5,
17
]
Compile: 350.837ms | Execution: 32.600ms | React with ❌ to remove this embed.
fifty Shucks
fifty ShucksOP9mo ago
thank you
Want results from more Discord servers?
Add your server