PD,
When you have:
List<StructA>
List<ClassA>
In a method, the references are the only thing on the stack. They both
point to objects on the heap. The internal storage for the items are array
references (which are on the heap as well) to either structures or classes.
There is really little difference in this case. There should be a
little overhead in using the generic list with a structure, which is because
you are copying the value from the list every time you are assigning it
(value types are copied when assigned, and passed as parameters, unless
passed by ref, which List<Tdoes not do).
If you want to see just how much of a performance difference there is,
then it's easy enough to test (use the Stopwatch class in the
System.Diagnostics namespace).
However, I would be more concerned with the semantics of using a
structure versus the performance implications, as doing this:
List<StructAmyList = ...;
StructA myItem = myList[0];
myItem.Property = value;
Compared to doing this:
List<ClassAmyList = ...;
ClassA myItem = myList[0];
myItem.Property = value;
Will have two different effects.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"J2EE Convert" <pi****@gmail.comwrote in message
news:11*********************@q5g2000prf.googlegrou ps.com...
Hello,
Sorry if this has already been beaten to death. However, I was not
able to find the answer in my searches. Is there any performance trade
off between creating and using a generic list of a Class Type and a
generic list of a Struct Type assuming both types are the same save
for one being a structure and one being a class. See below.
List<StructAmList;
List<ClassAmList;
Assuming that ClassA and StructA are exactly the same see below.
//Each type has 2 strings and a getter property for each string.
string mFirstName;
string mLastName;
My initial thought was this is a good use for a structure however I am
not sure what the implications of adding such a structure to a generic
list is. Is there any overhead with moving the structure from the
stack to heap since it is now contained within a reference type etc..
I know it may be a crappy example but I am trying to get my head
around it.
PD