C# The 'new' keyword on existing objects

后端 未结 9 490
一整个雨季
一整个雨季 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:44

    If there are no other references to Red car, it will be collected by the GC on its next cycle. You don't need anything extra (unless it's a class that has streams etc. that need to be disposed)

提交回复
热议问题