Class vs Struct IN THIS CONTEXT

Joined
Apr 19, 2020
Messages
7
Programming Experience
1-3
Hi, everybody.

I understand the differences between structure and class. But not completely in the hardware / deep sense.

Structure is the type of value. But can there be a structure that has a link field?

That is, the essence of the question, in this context, is it correct to use the structure and will there be a plus in optimization ?

1588060408131.png
 
Structures should generally be used for small, immutable types. If your type is not small, which means more than about 4 or 5 fields, or is not immutable, i.e. property values can be changed, then you should probably be using a class. Generic structures are OK although a bit unusual. There's no problem with a structure having reference-type (class) fields/properties.
 
What our OP has essentially recreated in post #1 is the class Tuple<T> and the struct ValueTuple<T>. To get a little background as to why the ValueTuple came about see the "Struct or class" section of the Design Notes for C# 7.0's tuple support. Alternative see this StackOverflow answer comparing the two flavors of tuples:
 
Back
Top Bottom