Accessing a List<Struct>in C#

So I have this Struct List:

private struct Room
{
		int aRoomID;
		float aX;
		float aY;
		float aZ;
		int aAngle;
}
private List<Room> MyRooms = new List<Room>();

Can someone explain how I would go about accessing the list?

Lists use the same syntax as built-in arrays for accessing indexes. Like so:

Debug.Log (MyRooms[0].aRoomID.ToString());

I thank you, sir!

You might be best to use a class not a struct, unless you have good reason for a struct, you should avoid them as they can cause potential issues with optimization if used in-correctly. Always use class and convert to struct if need be in the future.

But what about the GC that come with classes? Structs don’t have any issues with it.

I saw a discussion on this before, i will try to find it.

http://www.dotnetperls.com/struct-versus-class

http://www.dotnetperls.com/optimization

That is not a valid reason to necro-post something from NINE years ago!

Structs CANNOT be modified “in place” if they are part of some other object such as a List or any object that returns the struct via getter.

This is to preclude you from misunderstanding what you are doing, which WOULD be modifying and discarding your modifications, which would certainly prove baffling. That’s why it is an error.

Steps to success:

  • please don’t necro-post
  • copy the struct to a local variable struct
  • modify any / all fields you want to change in the local variable
  • copy the local variable struct back to where you got it.

Further reading on the topic:

Value Types vs Reference Types:

1 Like

Sorry about that. I will try to only post to more modern posts problems in the future. It seems it sort of works like I want. It seems I have forgotten more about basic programming than I thought. Time to break out the text books.

It happens fast my friend. I went on a three week no-computer vacation and it took me a month after I got back before I was fully comfortable again.

I’d like to think I am actually not stupid :slight_smile: and that this is just the reality of the complexity of things we use every day in modern software engineering.

The only thing that seems to help is to relentlessly reverify things back to the source documentation whenever anything untoward occurs.

If you’re fast at the docs and barely remember anything about the API, you’re still way better than if you think you know how it works and get errors and stubbornly insist that what you are doing is reasonable.

2 Likes

Oh man, thank you for reassuring thoughts. I have huge memory lapse issues due to the amount of stuff I have in my head. Garbage collection just takes over at times and zaps huge chunks of whatever lived there. I can still remember my name and general intentions in life, so I wouldn’t call it critical or threatening, but it’s hard not to consider oneself not-smart if you keep forgetting what you did last week, for the whole week, but it’s not like amnesia, I lose only the implementation details.

It might be because I’m long past my point of burning out, it’s been 15 years or so, without any vacation whatsoever, and I think it’s just a mechanism that as soon as I start to think about another contingency everything else is wiped out after only a day or so. Normally they say people on a vacation need at least 3-4 days to fully leave their work and begin to rest mentally, well my brain seems to be greedy and treats anything that lasts for more than 4 hours as a distraction worthy of a “vacation” label.

So to have any resemblance of self-esteem one has to negotiate with ones own brain I guess.

brain: “I don’t know this, let’s do something else”
me: “What do you mean you don’t know this, we did this last week, it had math and it was very complex and we solved everything.”
brain: “No.”
me: “C’mon, look, here’s a folder, look at this code.”
brain: “No. I mean, yes I can see vaguely it was something that interested me, but that was something I did a long long time ago and I don’t have a clue about it. Nada.”
me: “Look at the dates, it was literally three days ago.”
brain: “I can’t make anything out of it. See? Why didn’t you add any comments? This code is shit.”
me: “Now that you mention it, it is a bit difficult… Can’t you remember when we ruled over this? It was a breeze.”
brain: “Only vaguely, it did happen, yes, but then we went on a vacation…”
me: “It wasn’t a vacation, I had to move out from the apartment and it was very stressful…”
brain: “Same thing. Vacation!”

2 Likes