Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of Structs or Dataset in C#?

I have some data I want to work with. Two string and two numbers and I have say 8000 rows of data.

Is a dataset the best option here to use, or could I use a struct and have a list of structs?

Would there be much performance difference between the list and the dataset?

like image 407
ghost Avatar asked Dec 08 '25 12:12

ghost


2 Answers

DataSets and DataTables are often more verbose and have some overhead to access, but usually are interoperable with whatever data-binding sort of stuff you're using.

If you have the choice (i.e. you're not hooked to some component that uses DataTables) I'd strongly suggest using an appropriate strongly-typed collection, like a generic List, Dictionary, or SortedDictionary. I propose that the flexibility and transparency will benefit you in the long run, if there is a long run to your project.

P.S. Two strings and two numbers is big enough that I doubt you'll see any benefit from making it a struct instead of a class. Of course, you should profile it for yourself, but that's my intuition. I agree with the post mentioning that you should be sure you understand the fundamental differences between the two if this is a case of premature optimization.

like image 191
mqp Avatar answered Dec 11 '25 03:12

mqp


Ok, I'm extrapolating based on limited data, but the fact that you are asking between a list of structs and a dataset implies to me that you're somewhat new to C# and have not been introduced to the fact that a struct is a ValueType and therefore lives on the stack. You have probably heard that a struct grants you "better performance" somewhere, and want to get the best performance you can for your list of 8000 items.

First, I believe that you are prematurely optimizing. Don't. Do whatever works within the scope of your program. If you are building this list of 8000 items yourself programmatically, perhaps from an XML or flat file, I'd suggest you use a list of objects, as that will be the easiest for you to program against. Your post does not imply that you have a relationship between two tabular sets of data, so a DataSet would be unnecessary.

That said, if you are receiving that list from the database somehow, and your current data layer is ADO.NET 2.0 (and therefore returns DataSets and DataTables), then I'd say use that. If you are receiving the list from the database but your data layer is not yet defined, I would suggest you look into LINQ to SQL or Entity Framework.

Again, I caution you against prematurely optimizing, especially given that you don't appear to understand how structs work. Again this is an assumption and I apologize in advance if I am wrong.

like image 28
Randolpho Avatar answered Dec 11 '25 01:12

Randolpho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!