Generic List and static variable behaviour in c# -
i have simple application in c#. when ran code not getting expected result?.i getting 2,2,1 expecting 1,2,3
using system; using system.collections.generic; using system.linq; using system.text; namespace consoleapplication12 { class program { static void main(string[] args) { mylist<int> list1 = new mylist<int>(); mylist<int> list2 = new mylist<int>(); mylist<double> list3 = new mylist<double>(); console.writeline(list1.getcount()); console.writeline(list2.getcount()); console.writeline(list3.getcount()); } } public class mylist<t> { static int _count; public mylist() { _count++; } public int getcount() { return _count; } } }
the result expect
2 2 1
this msdn blog post tells
a static variable in generic class declaration shared amongst instances of same closed constructed type (§26.5.2), not shared amongst instances of different closed constructed types. these rules apply regardless of whether type of static variable involves type parameters or not.
Comments
Post a Comment