Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [C#] arrays

This thread is locked; no one can reply to it. rss feed Print
[C#] arrays
CursedTyrant
Member #7,080
April 2006
avatar

I just recently started learning C# (to try out XNA, of course ;D), 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.

---------
Signature.
----
[My Website] | [My YouTube Channel]

CGamesPlay
Member #2,559
July 2002
avatar

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.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

CursedTyrant
Member #7,080
April 2006
avatar

Thanks! That should make things much more easier ;D

---------
Signature.
----
[My Website] | [My YouTube Channel]

ixilom
Member #7,167
April 2006
avatar

As another C# beginner, I got a question about Dispose() :P

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?

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

CGamesPlay
Member #2,559
July 2002
avatar

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.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Thomas Fjellstrom
Member #476
June 2000
avatar

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)?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

CGamesPlay
Member #2,559
July 2002
avatar

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.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

CursedTyrant
Member #7,080
April 2006
avatar

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?

---------
Signature.
----
[My Website] | [My YouTube Channel]

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
Gillius's Programming -- https://gillius.org/

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.
The second line loses the reference to aObjects[0], making it elligible for garbage collection.

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.
Second line allocates a new instance and assigns it to the reference.
Third line reassigns the reference, making the previous instance eligible for GC.

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 ;) (Yes, nitpicking here :P)

Go to: