![]() |
|
[C#] arrays |
CursedTyrant
Member #7,080
April 2006
![]() |
I just recently started learning C# (to try out XNA, of course 1. Is there something like vector<> in C#? If so, what is it and how do I use it? Mostly, I just want a vector<> or something like that, because arrays aren't the best choice for things like bullets etc. --------- |
CGamesPlay
Member #2,559
July 2002
![]() |
Quote: 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. Quote: 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. -- Ryan Patterson - <http://cgamesplay.com/> |
CursedTyrant
Member #7,080
April 2006
![]() |
Thanks! That should make things much more easier --------- |
ixilom
Member #7,167
April 2006
![]() |
As another C# beginner, I got a question about Dispose() Is there some generic rule about which objects implement the IDisposable interface? ___________________________________________ |
CGamesPlay
Member #2,559
July 2002
![]() |
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. -- Ryan Patterson - <http://cgamesplay.com/> |
Thomas Fjellstrom
Member #476
June 2000
![]() |
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)? -- |
CGamesPlay
Member #2,559
July 2002
![]() |
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. -- Ryan Patterson - <http://cgamesplay.com/> |
CursedTyrant
Member #7,080
April 2006
![]() |
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? --------- |
gillius
Member #119
April 2000
|
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. Gillius |
Audric
Member #907
January 2001
|
CObject[] aObjects = new CObject[5]; aObjects[0] = new CEnemy(); aObjects[0] = new CEnemy2();
Actually, The first line already allocates 5 CObjects. |
monkeyCode
Member #7,140
April 2006
|
Object[] objects = new Object[5]; objects[0] = new Enemy(); objects[0] = new Enemy();
Quote: Actually, The first line already allocates 5 CObjects. Wrong. The first line creates an array of objects, they are not yet initialized to anything. |
Audric
Member #907
January 2001
|
Argh you're right... CObject isn't a value type, so the array will only be initialized to null "pointers". |
monkeyCode
Member #7,140
April 2006
|
null references |
|