Wednesday 15 June 2011

C# Array or List for a single Class type -



C# Array or List for a single Class type -

i have class contains few basic types. (3x float, 2x int).

now need collection can hold several 1000000 instances of class. need no derived types. elements single class. farther more number of elements fixed. in cases plan re-create whole list/array , modify copy. list/array shall immutable, don't need syncronize other threads.

now question are:

do benefit array instead of list? do save memory using array? what speed?

i read list in c# internally implemented array well.

if c++, know array hold finish object. i'm not sure how c# handles this. c# array hold references class instances or hold finish datastructure?

the list/array shall immutable, don't need synchronize other threads.

did consider immutable collection instead of t[] or list<t>? immutablearray<t> create sense. can utilize immutablearray<t>.builder create collection in efficient way.

do benefit array instead of list?

if don't need number of elements alter should utilize array. create clear looks @ code you're not changing number of elements.

do save memory using array?

it depends how you'd create list<t>. internally, when add together elements list<t> 1 1 underlying array's size changes using 2* multiplier: when there not plenty space new element current underlying array replaced new 1 twice size. yes, can save memory using array directly, because won't have unnecessary memory allocated. however, can accomplish same using list<t>, either creating using constructor takes list capacity or calling trimexcess method after elements added list.

what speed?

using array you'll save logic makes list<t> methods, properties , indexer property calls translated underlying array calls. shouldn't care that, unnoticeable.

if c++, know array hold finish object. i'm not sure how c# handles this. c# array hold references class instances or hold finish datastructure?

it depends. if define type reference type (a class) both array , list hold reference particular items. if define value type (a struct), array hold actual elements.

c# arrays list

No comments:

Post a Comment