C# The 'new' keyword on existing objects

后端 未结 9 504
一整个雨季
一整个雨季 2020-12-28 13:25

I was wondering as to what happens to an object (in C#), once its reference becomes reassigned. Example:

Car c = new Car(\"Red Car\");
c = new Car(\"Blue Car         


        
9条回答
  •  庸人自扰
    2020-12-28 13:37

    You create a new object and assign a reference to it to your variable c. At the same time the previous object (the "red car") is now not referenced anymore and may be garbage collected.

提交回复
热议问题