c# - Dictionary with a class as the value. -
i have small class use value in dictionary. when delete dictionary destroy class instance?
class program { class test { public string a_string; public string b_string; } static dictionary<int, test> _dict = new dictionary<int, test>(); static void main(string[] args) { for(int x = 0; x <=5; x++) { test _test = new test(); _test.a_string = "a" + x.tostring(); _test.b_string = "b" + x.tostring(); _dict.add(x, _test); } _dict.remove(2); } }
no. can access test long in scope. since each instance of test goes out of scope after each cycle of loop, can't if remove dictionary.
static void main(string[] args) { for(int x = 0; x <=5; x++) { test _test = new test(); _test.a_string = "a" + x.tostring(); _test.b_string = "b" + x.tostring(); _dict.add(x, _test); // each _test goes out of scope here } _dict.remove(2); // removed dictionary, have no way access now. i have assumed question whether can access it. if "deletion" in unmanaged memory sense, forget it, there no deterministic deletion, that's garbage collectors for.
Comments
Post a Comment