Is there reference count optimization ? RRS feed

  • Question

  • So i wonder, does NET optimize reference count in obvious cases ?:

    All we know that C# has reference counting for Reference types, and when we copy reference, it makes additional overhead actions like: incrementing and decrementing.

    BUT, obvously most cases do not need such overhead actions, for example:


    class TestClass
    	List<int> X = new List<int>();	// X.RefCount++ no question, its for sure
    	void Test()
    		DoSome(X);		// here the question
    		X = null;		// X.RefCount-- no question, its for sure
    	void DoSome( List<int> Y )
    		// is there X.RefCount++ ??? here the question
    		Y.Add(10);	// we dont store Y reference anywhere, we just use it inside DoSome function.
    		// is there X.RefCount-- ??? here the question


    As we see DoSome function just make some local action on reference copy of object X,

    so can C# recognize this and do not do reference count overhead when X is copied to Y ?

    • Edited by NewbTom Tuesday, October 18, 2011 2:57 PM
    Tuesday, October 18, 2011 2:47 PM


All replies