G
Giovanni Dicanio
Hi,
please consider the following scenario:
I allocate some objects in C# using new.
e.g.:
MyObject x = new MyObject();
then I assign this object to several places, e.g. in Tag field in
ListViewItem, in some collection like List<MyObject>, etc.
The point is that the same 'x' is assigned to several different places.
If I clear the listview control, can I be sure that the List<MyObject>
container still stores reference to 'x' object?
And if I clear the List<MyObject> container, can I be sure that the listview
control ListViewItem.Tag field stores valid reference to 'x' object?
The point is that I have a C++ background, and in C++ I can explicitly
control pointers to objects (or use smart pointers wrappers like
shared_ptr<T> template), instead C# abstracts all that from me.
I'd like know if I can trust the C# run-time in scenarios like one described
above, i.e. the garbage collector does not destroy objects that I'm using in
other contexts of the same application.
Or is there some kind of explicit reference counting mechanism that I should
use in C#, like COM IUnknown::AddRef and Release?
i.e. should I call something like x.AddRef() when I store 'x' somewhere, and
x.Release() when I do not need 'x' anymore in that particular place?
Last question:
Is it possible to have RAII in C#?
(This could be useful for classes that manage resources like a file...)
Is 'using' keyword the only option in C# for something similar to C++ RAII?
Thank you very much,
Giovanni
please consider the following scenario:
I allocate some objects in C# using new.
e.g.:
MyObject x = new MyObject();
then I assign this object to several places, e.g. in Tag field in
ListViewItem, in some collection like List<MyObject>, etc.
The point is that the same 'x' is assigned to several different places.
If I clear the listview control, can I be sure that the List<MyObject>
container still stores reference to 'x' object?
And if I clear the List<MyObject> container, can I be sure that the listview
control ListViewItem.Tag field stores valid reference to 'x' object?
The point is that I have a C++ background, and in C++ I can explicitly
control pointers to objects (or use smart pointers wrappers like
shared_ptr<T> template), instead C# abstracts all that from me.
I'd like know if I can trust the C# run-time in scenarios like one described
above, i.e. the garbage collector does not destroy objects that I'm using in
other contexts of the same application.
Or is there some kind of explicit reference counting mechanism that I should
use in C#, like COM IUnknown::AddRef and Release?
i.e. should I call something like x.AddRef() when I store 'x' somewhere, and
x.Release() when I do not need 'x' anymore in that particular place?
Last question:
Is it possible to have RAII in C#?
(This could be useful for classes that manage resources like a file...)
Is 'using' keyword the only option in C# for something similar to C++ RAII?
Thank you very much,
Giovanni