+ 2

del multiple instance refs & calling ctor/dtor of collection items

1- there's some way to, when delete a certain instance, directly delete all its other references for a total deletion? 2- there's some way to call ctor & dtor of obj when added & removed in a collection? thx David Carroll

27th Oct 2020, 12:20 PM
Kiwwi#
Kiwwi# - avatar
6 Réponses
+ 1
Kiwwi# Regarding constructor calls: the constructor is only executed when the object is created. Regarding destructor calls: You can't forcibly destroy an object in managed code. That is to say, the CLR doesn't give that level of access outside of the Garbage Collector (GC). This is to prevent destroying objects that might be referenced somewhere else. Otherwise, this would open your code to potential runtime exceptions if the object was forcibly removed from memory while referenced elsewhere. I suppose you could do this in unmanaged C#, outside of the CLR, where memory would need to be manually managed. All said... it's not clear what scenario would involve calling the constructor again or forcibly destroying the object based on being added or removed from a collection. The object won't be aware of how it's referenced or where it's contained. So, the actions would need to be facilitated by an external object. This wouldn't ever just automatically happen.
27th Oct 2020, 9:37 PM
David Carroll
David Carroll - avatar
+ 1
Kiwwi# 1 - No need to keep track of references in C# managed code like in C++. I'm assuming this is what you might be thinking. Even if it isn't what you're thinking and perhaps you're just wanting the option, there's just no option to do so with the CLR. 2 - Regarding your comment: "... maybe must use base call on that objects cause they inherit and the type of the list is their base type" Your mistake is in thinking the object creation has anything to do with where the reference is assigned. Every instance of `new Derived("foo")` below instantiates exactly the same way regardless of where its reference is assigned. ---- Derived d = new Derived("foo"); Base b = new Derived("foo"); object o = new Derived("foo"); var list = new List<Base>(); list.Add(new Derived("foo")); list.Add(d); list.Add(b); list.Add(o); var list = new List<Base> { new Derived("foo")) }; var list = new List<Derived>(); list.Add(new Derived("foo")); var list = new List<Derived> { new Derived("foo")) };
28th Oct 2020, 5:26 AM
David Carroll
David Carroll - avatar
+ 1
Regarding your comment: ---- "so there's really only one instance?" -- Not in my examples or in the for loop you just posted. I feel like the more I try to simplify things, the more confused I'm making you. 🤔 The example below results in 5 new object instances. //5 constructor calls for (x5) list.Add( new obj() ); The lines below are essentially doing the same thing creating 5 new instances each: //5 constructor calls list.Add( new obj() ); list.Add( new obj() ); list.Add( new obj() ); list.Add( new obj() ); list.Add( new obj() ); This is different from the following: //1 constructor call var o = new obj(); for (x5) list.Add( o ); //1 constructor call var x = new obj(); list.Add( x ); list.Add( x ); list.Add( x ); list.Add( x ); list.Add( x ); ---- My previous response was to explain there is no difference in the constructor behavior when assigning to a base type or a List declared with a base type. This was in response to your earlier post, which I quoted in my previous response.
28th Oct 2020, 6:14 AM
David Carroll
David Carroll - avatar
+ 1
LOOL I already know all that so u make me think that I was wrong while I was not. a little miss understanding, maybe I was not enough clear about, and u don't know what I know and what not. the reason behind it is that in my code when I create on the same add statement, the ctors were not called, but solved it using base() in derived ctor (to use base ctor to show a message of construction for all the derived types instead of reapeat it for every derived class, using GetType().Name), the type of the list were list<base> but couldn't realize why dtor not call were remove from list. maybe cause they have more references in other place (I dobt remember 100% cause its a couple of days ago code) the next I will prepare first my explanations instead of write here directly.. (I must recognize that it get me so confused xD)
28th Oct 2020, 12:47 PM
Kiwwi#
Kiwwi# - avatar
0
David Carroll so.. 1- the best way is keep track of all its references abd delete them all together 2- I explain it more clearly; I create objects adding them to a list in a loop, so I expect their ctor would be called during that creation, maybe must use base call on that objects cause they inherit and the type of the list is their base type
28th Oct 2020, 4:45 AM
Kiwwi#
Kiwwi# - avatar
0
so there's really only one instance? my logic was if I instantiate 5 obj they may call their ctors what I was doing were create them in the same assignation: for (x5) list.Add( new obj() ); // should call ctor x5
28th Oct 2020, 5:36 AM
Kiwwi#
Kiwwi# - avatar