简体   繁体   中英

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?

DataSet s and DataTable s 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 (ie you're not hooked to some component that uses DataTable s) 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.

PS 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.

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.

Depends on what "work with" means to you. If I were displaying the data in a DataGridView or some other control such as that, I'd opt for the DataSet/DataTable route. Provides you with the ability do some easy filtering of the data.

Again, the performance difference of the List versus the DataSet depends on what you are doing. Lists are great for iterating through the container and things of that nature. DataSets and DataTables provide for easy operations such as ReadXML() (assuming you've got your data in XML, of course) that will take your elements, create columns and fill in the rows for you. That's pretty convenient.

Unless you are running on pretty restrictive hardware or have a lot else going on by other threads/processes, 8000 items isn't really a terribly large amount of data. I'd choose my container based on what I planned to do with it. If I were using databinding, I'd opt for the DataSet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM