0

Structure instead class in c#

when it is worth using a structure instead of the class, and why?

8th Jun 2018, 5:30 AM
Zsolt Kelemen
Zsolt Kelemen - avatar
4 Réponses
+ 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
8th Jun 2018, 5:40 AM
ifl
ifl - avatar
+ 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
8th Jun 2018, 5:48 AM
ifl
ifl - avatar
+ 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
8th Jun 2018, 6:34 AM
ifl
ifl - avatar
0
I understand this, but why use struct in this case? why don’t use class?
8th Jun 2018, 6:15 AM
Zsolt Kelemen
Zsolt Kelemen - avatar