0
Structure instead class in c#
when it is worth using a structure instead of the class, and why?
4 ответов
+ 1
This question is asked here for c++ , some of the comments apply to c# also.. https://stackoverflow.com/questions/54585/when-should-you-use-a-class-vs-a-struct-in-c
+ 1
I usually use a struct when I need to manipulate a simple group of data types without the need for constructor or other methods.
For example, a set of coordinates (a Point) does not really need a class
+ 1
Having a class is a bit of an overhead. also an object is a reference, whereas a struct is a value type. This makes a difference on where things are in memory, and when you copy the data to a new variable.
For more info see this article.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-structs
And also
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/index
0
I understand this, but why use struct in this case? why don’t use class?