1

I tend to use ArrayLists of structures. It is then very easy to cycle through the list with a foreach.

The problem I have is I cant use a foreach to modify the structures contents and have to use a for and messy typecasts.

((dataStructure)files[x]).name = here;

Is there a tidier way to do it?

Tim
  • 7,746
  • 3
  • 49
  • 83

4 Answers4

13

Yes, there is: don't use an untyped ArrayList, these types are deprecated in favour of the generic types in System.Collections.Generic. In your case: List<T>.

You still can't use it in conjunction with a foreach loop to modify structure values but at least you don't have to cast.

Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Lists do look like a much better idea – Tim May 14 '09 at 12:21
  • If you're stuck with your ArrayLists for some reason (they get returned by some API that you don't have control over) then use the Cast extension method for ArrayList to convert them into an IEnumerable. IEnumerable foo = foo1.Cast(); – Martin Peck May 14 '09 at 12:28
7

I know this sounds simplistic but just say no to mutable value types.

They're almost never the right solution to the problem. There are a very few exceptions, but classes are almost always the way to go.

In addition, if you're really using an ArrayList then you'll be incurring an unboxing cost already... (As Konrad says, if you can use .NET 2.0 then use generics.)

If you really insist on using mutable structures, then use a for loop instead of foreach. But please, please change to classes anyway.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Jon... I think you should just put a disclaimer in every post you write about mutable types :D – Jorge Córdoba May 14 '09 at 12:27
  • i need to store various details of all the hosts that i have discovered and i need to be able to cycle through and modify them. how shoudl i be doing this? – Tim May 14 '09 at 12:27
  • Use a class instead of a struct. – Jon Skeet May 14 '09 at 12:29
  • Have a list of instances of the class. – Jon Skeet May 14 '09 at 12:36
  • I'll start doing that then. I assumed structs werent that different to classes as they seem pretty similar. – Tim May 14 '09 at 13:05
  • No, they behave very differently. I *strongly* recommend that you get a good beginner's guide to C# - you'll run into some really weird problems before you understand value types vs reference types. – Jon Skeet May 14 '09 at 13:14
  • Ive got a c# book which ive read most of. I didnt mean that they are the same as clearly they are not. They appear similar as they can both have variables and methods. Ive come from an embedded c background where you store data in structs, so you can see how it might feel a bit strange to be storing data in classes – Tim May 14 '09 at 13:26
  • What I'm saying is that the difference is really, really important. Try to forget you ever knew C :) – Jon Skeet May 14 '09 at 13:36
1

Use the generic version of ArrayList: List<DataStructure>.

This way things look a lot better:

files[x].name = here;
bruno conde
  • 47,767
  • 15
  • 98
  • 117
0

Yes, there are cases where List <T> also is not useful. In those cases the oldest trick in the manual works. Instead of foreach, you should use while loop:

while (listItem.Count >0)
{
//do operation with 0th element of List Item always like
 deletefunc(lisItem[0]);
}
Kushagra
  • 31
  • 1