Sign in to follow this  

C# Arrays

This topic is 4282 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm new to the way that arrays work in C#. I need to have an array to hold block objects for my Tetris game so that I can cycle through all of the existing blocks with a foreach loop. My question is, can I somehow add new blocks to the array without changing the size of the array manually? Or how do I continually add and take things out of the array easily?

Share this post


Link to post
Share on other sites
Grantyt3,

If you are interested in keeping track of a list that is frequently changed, you should look to use a System.Collections.* or System.Collections.Generic collection object to manage this for you. I think you would probably find the generic ArrayList to be your best bet (The non-generic ArrayList is OK, but it casts everything as a System.Object, so it's kind of annoying at times). Assuming your object was a TetrisBlock, you could do something like this:

using System.Collections.Generic;

ArrayList<TetrisBlock> blockList = new ArrayList<TetrisBlock>();

blockList.Add(new TetrisBlock("Whatever params you need"));
//etc, etc.

foreach (TetrisBlock block in blockList) {
}
// etc, etc.

blockList.Remove(0);
// Or
blockList.Remove(someTetrisBlock);



Keep in mind that you may not change a Collection that you are currently iterating, so you can't add or remove elements to an ArrayList if you are inside of a foreach loop that is using it.

Hope this clears things up,

-Dave

Share this post


Link to post
Share on other sites
I believe System.Collections.ArrayList is what you're looking for. With it, you can dynamically add, remove, insert, and much more. So essentially, it acts like a C++ vector (though, I've just begin reading up on vectors), only that it's not constraint to a single type for a given instance like a C++ vector. It's usage is also like a C++ vector (array index notation).

- xeddie

Share this post


Link to post
Share on other sites
Quote:
Original post by Grantyt3
I'm new to the way that arrays work in C#. I need to have an array to hold block objects for my Tetris game so that I can cycle through all of the existing blocks with a foreach loop. My question is, can I somehow add new blocks to the array without changing the size of the array manually? Or how do I continually add and take things out of the array easily?

Thirding my recommendation for ArrayList.

Share this post


Link to post
Share on other sites

This topic is 4282 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this