1

Possible Duplicate:
C#: Structs versus classes

I am creating a library, which is relatively small to require a database, containing tags of MP3 files.

I know that it would be better to stick with structures if I have fixed size data like integers, doubles etc... However in my case, I have strings and lists that contains strings and even other structures... If I will go with structures, it will look like this:

Public Structure STitle
    Dim Title As String
    Dim Path As String
End Structure

Public Structure SAlbum
    Dim Album As String
    Dim Songs As List(Of STitle)
End Structure

Public Structure SArtist
    Dim Artist As String
    Dim Albums As List(Of SAlbum)
End Structure

Then, I will have something like this:

Public SongLibrary As New List(Of SArtist)

The strings and lists are confusing me, especially speed-wise. Since I will be searching through lots of song, I want to make sure that I am using the fastest one.

I am also confused about boxing and unboxing. I assume that the list of string contains only the pointers to "the string" and same for the structures(trying to think low level). However, I don't know whether boxing-unboxing, which slows down the searching(a reason to not to use arraylists), takes place or not since the sizes are impossible to determine(or is it?)

So which one to pick? Class, or structure? or is there something else suiting better?

Community
  • 1
  • 1
gunakkoc
  • 1,069
  • 11
  • 30
  • 3
    Keep in mind that using a structure doesn't just make everything faster, like magic. In your case, there is unlikely to be a performance gain from using a `struct`. – Servy Jan 18 '13 at 16:49

2 Answers2

7

I know that it would be better to stick with structures if I have fixed size data like integers, doubles etc

How do you "know" that? In my experience, it's very rarely a good idea to create a custom value type. It does happen - there are plenty in my Noda Time project, for example - but usually I would be wary. I'd also be very wary before exposing mutable structures.

I am also confused about boxing and unboxing. I assume that the list of string contains only the pointers to "the string" and same for the structures(trying to think low level).

For structures, there isn't a pointer (reference). A List(Of Integer) is implemented with an Integer() for example. The array contains the values directly. With generic collections, no boxing is required.

See the class design guidelines for more information, but basically I'd default to using classes unless I had a really compelling reason to create a structure.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I think I know it from http://msdn.microsoft.com/en-us/library/vstudio/ms229017(v=vs.100).aspx – gunakkoc Jan 18 '13 at 16:48
  • @GunDeniz: Your "if I have fixed size data" doesn't correlate with the guidance in that link. It says nothing about logically representing a single value or being immutable, for example. – Jon Skeet Jan 18 '13 at 16:49
  • @JonSkeet: would you put a short list of _really compelling reason(s)_. Thanks – Sunny Milenov Jan 21 '13 at 21:52
  • 1
    @SunnyMilenov: Well, that link gives them really - but I definitely stick to making them immutable, and representing a single logical value. Prior to Noda Time, I'd very rarely created any custom structs. There *are* times where they give significant performance boosts, but that should only come after profiling etc. – Jon Skeet Jan 21 '13 at 21:55
2

The strings and lists are confusing me, especially speed-wise. Since I will be searching through lots of song, I want to make sure that I am using the fastest one

not clear what is relation between string and list in your case, but the fast search often related to the how data is destributed in your collection.

Fastest, usually is: having sorted collection and run binary search over it.

Especially from the point of view of fast data retrieval do not use structs, as in the moment you retrive the data (in case of structs) from the collection, the data will be copied to another, new object. In case of classes, instead, you wil be given a pointer (much efficient).

Use reference types (classes) and not structs

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • Hash based searching is usually much faster than a binary search on a sorted collection, as long as the type can be effectively hashed (quickly). – Servy Jan 18 '13 at 16:50
  • I meant accessing a structure from a list that contains a list of string or even another structure. It is going like x->y->z->t. The question was, which is faster accessing structures or accessing classes or something else? – gunakkoc Jan 18 '13 at 16:53
  • @GunDeniz: performance subject is very context related, but in general, if you warry about searching performance, stick with classes, for the reason explained in the answer. – Tigran Jan 18 '13 at 16:54
  • 2
    @GunDeniz: Do you have any evidence that the difference between structs and classes is actually going to be significant in your application? Any IO is likely to dwarf this sort of thing... you should design your API for clarity and ease of use, rather than trying to micro-optimize at this stage. – Jon Skeet Jan 18 '13 at 17:43