Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need struct? (C#)

Tags:

c#

To use a struct, we need to instantiate the struct and use it just like a class. Then why don't we just create a class in the first place?

like image 332
user133466 Avatar asked Aug 01 '09 16:08

user133466


People also ask

Why do we need struct?

In C#, a structure is a value type data type. It helps you to make a single variable hold related data of various data types. The struct keyword is used for creating a structure.

What is purpose of a struct class?

Classes and Structs (C++) Classes and structs are the constructs whereby you define your own types. Classes and structs can both contain data members and member functions, which enable you to describe the type's state and behavior.


3 Answers

A struct is a value type so if you create a copy, it will actually physically copy the data, whereas with a class it will only copy the reference to the data

like image 114
Paul Avatar answered Oct 27 '22 06:10

Paul


A major difference between the semantics of class and struct is that structs have value semantics. What is this means is that if you have two variables of the same type, they each have their own copy of the data. Thus if a variable of a given value type is set equal to another (of the same type), operations on one will not affect the other (that is, assignment of value types creates a copy). This is in sharp contrast to reference types.

There are other differences:

  1. Value types are implicitly sealed (it is not possible to derive from a value type).
  2. Value types can not be null.
  3. Value types are given a default constructor that initialzes the value type to its default value.
  4. A variable of a value type is always a value of that type. Contrast this with classes where a variable of type A could refer to a instance of type B if B derives from A.

Because of the difference in semantics, it is inappropriate to refer to structs as "lightweight classes."

like image 21
jason Avatar answered Oct 27 '22 06:10

jason


All of the reasons I see in other answers are interesting and can be useful, but if you want to read about why they are required (at least by the VM) and why it was a mistake for the JVM to not support them (user-defined value types), read Demystifying Magic: High-level Low-level Programming. As it stands, C# shines in talking about the potential to bring safe, managed code to systems programming. This is also one of the reasons I think the CLI is a superior platform [than the JVM] for mobile computing. A few other reasons are listed in the linked paper.

It's important to note that you'll very rarely, if ever, see an observable performance improvement from using a struct. The garbage collector is extremely fast, and in many cases will actually outperform the structs. When you add in the nuances of them, they're certainly not a first-choice tool. However, when you do need them and have profiler results or system-level constructs to prove it, they get the job done.

Edit: If you wanted an answer of why we need them as opposed to what they do, ^^^

like image 35
Sam Harwell Avatar answered Oct 27 '22 07:10

Sam Harwell