I just recently started learning C# (to try out XNA, of course ), and I've a couple of questions, which I couldn't find answers to anywhere in the documentation.
1. Is there something like vector<> in C#? If so, what is it and how do I use it?
2. Do I need to somehow destroy the objects in such vector (I'm asking because C# doesn't use pointers), or do they get deleted automatically?
Mostly, I just want a vector<> or something like that, because arrays aren't the best choice for things like bullets etc.
1. Is there something like vector<> in C#? If so, what is it and how do I use it?
Yes, see System.Collections.Generic, particularly the List class.
2. Do I need to somehow destroy the objects in such vector (I'm asking because C# doesn't use pointers), or do they get deleted automatically?
All objects are garbage collected automatically, although there are some objects, those which implement the IDisposable insterface, which need to be disposed manually. You can use a using statement to help with the process.
Thanks! That should make things much more easier
As another C# beginner, I got a question about Dispose()
Is there some generic rule about which objects implement the IDisposable interface?
Or is it something I have to check myself for each object I plan to use in the future?
Any object that opens an expensive handle will generally need to be IDisposable. For example, database connections, network connections, window handles, etc. Your images may, depending (I'm not familiar with XNA), as might your textures.
Wait, you have to manually dispose of IDisposable objects? Isn't that a little backwards? I don't get it to be honest, why can't the GC handle calling the destructors properly (where complex objects clean up after them selves)?
Because the GC might not run for a week depending on the conditions of your application. Also, the GC doesn't know how to close a database connection or window handle, it can only forget about it.
The .NET runtime correctly handles destructors, but it has some limitations (obviously, it requires a GC run). For more information, see Object.Finalize.
So basically... If I had a base class CObject, and two classes CEnemy and CEnemy2, how would this work?:
CObject[] aObjects = new CObject[5]; //random number, really aObjects[0] = new CEnemy(); aObjects[0] = new CEnemy2();
Does CEnemy get lost in memory, deleted, replaced by CEnemy2 or what? Is it safe/efficient to do it this way, or should I do it differently?
IDisposable really is for things like DB connections, and things that acquire native resources (like Win32 handles) that need to be freed. Normal object creation and deletion is handled by the GC.
In your example, the original CEnemy object would be collected by the GC eventually, quite possibly never if you don't allocate enough memory. In short-lived programs in managed environments like Java, it's possible that the GC never runs and objects are never deleted, letting the memory be freed by the OS. I assume the same is true for .NET.
CObject[] aObjects = new CObject[5]; aObjects[0] = new CEnemy(); aObjects[0] = new CEnemy2();
Actually, The first line already allocates 5 CObjects.
The second line loses the reference to aObjects[0], making it elligible for garbage collection.
Object[] objects = new Object[5]; objects[0] = new Enemy(); objects[0] = new Enemy();
Actually, The first line already allocates 5 CObjects.
Wrong.
The first line creates an array of objects, they are not yet initialized to anything.
Second line allocates a new instance and assigns it to the reference.
Third line reassigns the reference, making the previous instance eligible for GC.
Argh you're right... CObject isn't a value type, so the array will only be initialized to null "pointers".
null references (Yes, nitpicking here
)