Y
Y
Ytsu Ytsuevich2015-02-09 23:30:48
Data Structures
Ytsu Ytsuevich, 2015-02-09 23:30:48

Do I need to use structs in C#?

Let's say I have data flying from the database
1) Id
2) Name
Task. Save this data somewhere...
Bottom line. I will keep it on the sheet.
Question: it is better to create a class or structure.
The structure is faster, but if the data is still stored in the sheet, then is there any point. Will I get % productivity from this?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2015-02-10
Protko @Fesor

you should get not a percentage (precisely a slight increase, if any, which I personally doubt) from performance, but convenience in the development and support of the application.

S
Sumor, 2015-02-10
@Sumor

Use classes if you don't see the benefit of using structs in your code.
I'm not sure that using a structure with a string inside a sheet is faster than using a similar class. Especially in comparison with time of access to a DBMS.
Learn more about structs on MSDN
Structs can be used:
1. If you have a relationship with unmanaged code where a similar struct is expected.
2. If the use of value types is critical to you.
At the same time, one should not forget about the peculiarities of their use, for example, that when assigning or passing to a function, the structure is copied. Surely you can write an example where, because of this, the performance of working with a large structure will be lower than with a class.

O
Oxoron, 2015-02-10
@Oxoron

Not the fact that you will get a performance boost. Much depends on the operations on the sheet. Structs have slow comparisons by default (hello reflections), for example. If you have to move your objects around a lot, classes also have an advantage (hello reference types).
Food for thought: Anonymous types are often used when working with LINQ; they just inherit from class.
And after that, there is a rule: never use structures unless you clearly know why you need it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question