You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: concepts/structs/about.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
# About
2
2
3
-
C# `struct`s are closely related `class`s.
3
+
C# `struct`s are closely related to `class`es.
4
4
They have state and behavior.
5
5
They can have the same kinds of members: constructors, methods, fields, properties, etc.
6
6
7
7
Fields and properties can be simple types, `struct`s or reference types.
8
-
`struct`s observe the same rules about scope, read/write rules and access levels as do `class`s.
8
+
`struct`s observe the same rules about scope, read/write rules and access levels as do `class`es.
9
9
10
10
```csharp
11
11
enumUnit
@@ -38,7 +38,7 @@ new Weight(77.5, Unit.Kg).ToString();
38
38
One of the main things to remember is that when one struct is assigned to a variable or passed as a parameter the values are copied across so changes to the original variable will not affect the copied one and vice versa.
39
39
In summary, `struct`s are **value types**.
40
40
41
-
This [article][class-or-struct] discusses the differences between `struct`s and `class`s.
41
+
This [article][class-or-struct] discusses the differences between `struct`s and `class`es.
42
42
You will see from the article that `struct`s tend to be lightweight and [immutable][structs-immutable] although this guidance is not enforced (by default) by the compiler or runtime.
43
43
44
44
There are a couple of things that you will come up against (and about which the compiler will remind you):
@@ -63,15 +63,15 @@ Although `struct`s cannot be derived from other `struct`s they can implement int
63
63
64
64
## Equality
65
65
66
-
Equality testing for `struct`s can often be much simpler than that for `class`s as it simply compares fields for equality by default.
66
+
Equality testing for `struct`s can often be much simpler than that for `class`es as it simply compares fields for equality by default.
67
67
There is no need to override `object.Equals()` (or `GetHashCode()`).
68
68
Remember that if you are relying on `Object.GetHashCode()` you must still ensure that the fields involved in generating the hash code (i.e. all the fields) must not change while a hashed collection is use.
69
69
Effectively, this means that structs used in this way should be immutable.
70
70
71
71
In contrast to the method, `Equals()`, there is no default implementation of the equality operators, `==` and `!=`.
72
72
If your `struct` needs them then you will have to implement them.
73
73
74
-
On the other hand, this [article][equality] describes how performance can be optimised by creating your own custom `Equals()` and `GetHashCode()` method as is often done with `class`s.
74
+
On the other hand, this [article][equality] describes how performance can be optimised by creating your own custom `Equals()` and `GetHashCode()` method as is often done with `class`es.
75
75
The difference in the case of this exercise was about 20% in a not very rigorous comparison but that may be on the low side because all the fields are of the same type - see below.
76
76
77
77
There are discussions on the [web][equality-performance] about speed improvements, where the `Equals()` method is not overridden, if all fields are of the same type.
0 commit comments