Structs are light versions of classes. Structs are value types and can be used to create objects that behave like the built-in types.
Structs share many features with classes but with the following limitations as compared to classes.
- Struct cannot have default constructor (a constructor without parameters) or a destructor.
- Structs are value type and copied on assignment.
- Structs are value types while classes are reference types.
- Structs can be instantiated without using a new operator.
- A struct cannot inherit from another struct or class, and it cannot be the base of a class. All structs inherit directly from System.ValueType, which inherits from System.Object.
- Struct cannot be base class. So, Struct types cannot abstract and are always implicitly sealed.
- Abstract and sealed modifiers are not allowed and struct member cannot be protected or protected internals.
- Function members in a struct cannot be abstract or virtual, and the override modifier is allowed only to the override methods inherited fromSystem.ValueType.
- Struct does not allow the instance field declarations to include variable initializers. But, static fields of a struct are allowed to include variable initializers.
- A struct can implement interfaces.
- A struct can be used as a nullable type and can be assigned a null value.
So, when to use struct or classes?
To answer this question, we should have good understanding of differences.
S.N |
Struct |
Classes |
1 |
Structs are value types, allocated either on the stack or inline in containing types. |
Classes are reference types, allocated on the heap and garbage-collected. |
2 |
Allocations and de-allocations of value types are in general cheaper than allocations and de-allocations of reference types. |
Assignments of large reference types are cheaper than assignments of large value types. |
3 |
In structs, each variable contains its own copy of the data (except in the case of ref and out parameter variables), and any operation on one variable does not affect another variable.
|
In classes, two variables can contain reference of the same object and any operation on one variable can affect another variable. |
In this way, struct should be used only when you are sure that -
- It logically represents a single value, like primitive types (int, double, etc.).
- It is immutable.
- It should not be boxed and un-boxed frequently.
In all other cases, you should define your types as classes.
e.g. Struct
- struct Location
- {
- publicint x, y;
- publicLocation(int x, int y)
- {
- this.x = x;
- this.y = y;
- }
- }
- Locationa = new Location(20, 20);
- Locationb = a;
- a.x = 100;
- System.Console.WriteLine(b.x)
- ;
The output will be 20. Value of "b" is a copy of "a", so "b" is unaffected by change of "a.x". But in class, the output will be 100 because "a" and "b" will reference the same object.
I think, this article clarifies most of the doubts about struct. If you find further queries about struct, please share with me so that everyone can have a clear understanding about this topic.