C
C#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 ???
A
Angius41d ago
A list is a reference type, so a reference to that list We can easily check that, though
M
MODiX41d 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.
FS
fifty Shucks41d ago
thank you
Want results from more Discord servers?
Add your server
More Posts