Why structures in C#? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why structures in C#?

Structures in C# are almost the same thing as class. In structures does not make any sense to me having a structures that have methods and also a constructor like a class. For me that is insane. Can you give me a example of that aplication and make a more deep differentiation between the structure and the class in C#. Not even in C that kind of thing in structures are allow.

16th Nov 2023, 4:18 PM
Werg Serium
Werg Serium - avatar
1 Answer
+ 4
Werg Serium there is a performance difference. I think the primary thing to understand about structs in C# is described very well in ChatGPT's words: "Structs are value types and are stored directly where they are declared, which can result in less memory overhead compared to classes." ChatGPT gave me five other differences that would help determine when a struct is more appropriate than a class. It is too long to paste here.
16th Nov 2023, 4:44 PM
Brian
Brian - avatar