c# - What will happen to a var type when code block ends? -



c# - What will happen to a var type when code block ends? -

what diference between 2 examples of code?

public test(int x) { list<int> list= new list<int>(); list<int> list1 = new list<int>(); list= createlist(x); list1 = createlist(x + 1); dostuff(list, list1); list.clear(); list = null; list1.clear(); list1 = null; }

is way code?

public test(int ncount) { var list = createlist(ncount); var list1 = createlist(ncount + 1); dostuff(list, list1); }

nothing.

for readability , hence future maintenance reasons, utilize sec one. it's much easier understand, , because of design of c# , clr, much of code in first illustration not required.

i'll seek explain mean. don't need clear or set null references local variables (the garbage collector you). need class-level fields, , not (again, garbage collector collect them when class goes out of scope).

consider focussing more on concept of objects beingness disposable, , don't worry memory usage/garbage collection aspects. emptying list or setting reference list null doesn't reclaim memory; memory still allocated until garbage collector reclaims @ indeterminate time in future.

there means forcefulness garbage collection, typically means you're doing wrong if need go far. if need worry memory in much detail, .net may not right selection of language problem domain.

also, using var or specifying type explicitly makes no difference whatsoever.

c# .net memory-management codeblocks var

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -