From 6f56a62c9f12493738ab62b3395a781b329d3f3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Tue, 10 Dec 2024 13:10:28 -0800 Subject: [PATCH 1/4] One line per sentence --- concepts/structs/about.md | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/concepts/structs/about.md b/concepts/structs/about.md index b57aa9f20d..4080dff66f 100644 --- a/concepts/structs/about.md +++ b/concepts/structs/about.md @@ -1,8 +1,11 @@ # About -C# `struct`s are closely related `class`s. They have state and behavior. They can have the same kinds of members: constructors, methods, fields, properties, etc. +C# `struct`s are closely related `class`s. +They have state and behavior. +They can have the same kinds of members: constructors, methods, fields, properties, etc. -Fields and properties can be simple types, `struct`s or reference types. `struct`s observe the same rules about scope, read/write rules and access levels as do `class`s. +Fields and properties can be simple types, `struct`s or reference types. +`struct`s observe the same rules about scope, read/write rules and access levels as do `class`s. ```csharp enum Unit @@ -32,9 +35,11 @@ new Weight(77.5, Unit.Kg).ToString(); // => "77.6Kg" ``` -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. In summary, `struct`s are **value types**. +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. +In summary, `struct`s are **value types**. -This [article][class-or-struct] discusses the differences between `struct`s and `class`s. 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. +This [article][class-or-struct] discusses the differences between `struct`s and `class`s. +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. There are a couple of things that you will come up against (and about which the compiler will remind you): @@ -46,21 +51,32 @@ As a result of points 1 and 3 above there is no way for the developer of a `stru ## Common structs -You will see from the documentation that there is a close relationship between primitives and structs. See [`Int32/int`][int32], for an example. A more conventional example of a`struct`is the type [`TimeSpan`][time-span]. +You will see from the documentation that there is a close relationship between primitives and structs. +See [`Int32/int`][int32], for an example. +A more conventional example of a`struct`is the type [`TimeSpan`][time-span]. -Instances of `TimeSpan` behave much like numbers with comparison operators like `>` and `<` and arithmetic operators. You can implement these operators for your own `struct`s when you need them. +Instances of `TimeSpan` behave much like numbers with comparison operators like `>` and `<` and arithmetic operators. +You can implement these operators for your own `struct`s when you need them. -One thing to note about `TimeSpan` is that it implements a number of interfaces e.g. `IComparable`. Although `struct`s cannot be derived from other `struct`s they can implement interfaces. +One thing to note about `TimeSpan` is that it implements a number of interfaces e.g. `IComparable`. +Although `struct`s cannot be derived from other `struct`s they can implement interfaces. ## Equality -Equality testing for `struct`s can often be much simpler than that for `class`s as it simply compares fields for equality by default. There is no need to override `object.Equals()` (or `GetHashCode()`). 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. Effectively, this means that structs used in this way should be immutable. +Equality testing for `struct`s can often be much simpler than that for `class`s as it simply compares fields for equality by default. +There is no need to override `object.Equals()` (or `GetHashCode()`). +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. +Effectively, this means that structs used in this way should be immutable. -In contrast to the method, `Equals()`, there is no default implementation of the equality operators, `==` and `!=`. If your `struct` needs them then you will have to implement them. +In contrast to the method, `Equals()`, there is no default implementation of the equality operators, `==` and `!=`. +If your `struct` needs them then you will have to implement them. -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. 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. +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. +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. -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. The difference in this exercise of including disparate fields was about 60%. This is not mentioned in Microsoft's documentation so that makes it an un-documented implementation detail, and it should be exploited judiciously. +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. +The difference in this exercise of including disparate fields was about 60%. +This is not mentioned in Microsoft's documentation so that makes it an un-documented implementation detail, and it should be exploited judiciously. ```csharp public bool Equals(Weight other) From d92474082b0e8dabdb87b77eeabfce3f610e75e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Tue, 10 Dec 2024 13:11:32 -0800 Subject: [PATCH 2/4] One sentence per line --- concepts/structs/introduction.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/concepts/structs/introduction.md b/concepts/structs/introduction.md index d131de15d0..eae65c9aaf 100644 --- a/concepts/structs/introduction.md +++ b/concepts/structs/introduction.md @@ -1,6 +1,8 @@ # Introduction -C# `struct`s are closely related `class`s. They have state and behavior. They have constructors that take arguments, instances can be assigned, tested for equality and stored in collections. +C# `struct`s are closely related `class`s. +They have state and behavior. +They have constructors that take arguments, instances can be assigned, tested for equality and stored in collections. ```csharp enum Unit From 22c1aa073e277c077f9c6c6660ee1eec56b9e42f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Tue, 10 Dec 2024 13:12:17 -0800 Subject: [PATCH 3/4] Small grammar fixes --- concepts/structs/introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/structs/introduction.md b/concepts/structs/introduction.md index eae65c9aaf..b6c8dd57f5 100644 --- a/concepts/structs/introduction.md +++ b/concepts/structs/introduction.md @@ -1,6 +1,6 @@ # Introduction -C# `struct`s are closely related `class`s. +C# `struct`s are closely related to `class`es. They have state and behavior. They have constructors that take arguments, instances can be assigned, tested for equality and stored in collections. From 554ce7b913b4ec8200cc4e7d75ac1198f55f87de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Tue, 10 Dec 2024 13:14:06 -0800 Subject: [PATCH 4/4] Small grammar fixes --- concepts/structs/about.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/concepts/structs/about.md b/concepts/structs/about.md index 4080dff66f..cce0016e6a 100644 --- a/concepts/structs/about.md +++ b/concepts/structs/about.md @@ -1,11 +1,11 @@ # About -C# `struct`s are closely related `class`s. +C# `struct`s are closely related to `class`es. They have state and behavior. They can have the same kinds of members: constructors, methods, fields, properties, etc. Fields and properties can be simple types, `struct`s or reference types. -`struct`s observe the same rules about scope, read/write rules and access levels as do `class`s. +`struct`s observe the same rules about scope, read/write rules and access levels as do `class`es. ```csharp enum Unit @@ -38,7 +38,7 @@ new Weight(77.5, Unit.Kg).ToString(); 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. In summary, `struct`s are **value types**. -This [article][class-or-struct] discusses the differences between `struct`s and `class`s. +This [article][class-or-struct] discusses the differences between `struct`s and `class`es. 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. There are a couple of things that you will come up against (and about which the compiler will remind you): @@ -63,7 +63,7 @@ Although `struct`s cannot be derived from other `struct`s they can implement int ## Equality -Equality testing for `struct`s can often be much simpler than that for `class`s as it simply compares fields for equality by default. +Equality testing for `struct`s can often be much simpler than that for `class`es as it simply compares fields for equality by default. There is no need to override `object.Equals()` (or `GetHashCode()`). 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. Effectively, this means that structs used in this way should be immutable. @@ -71,7 +71,7 @@ Effectively, this means that structs used in this way should be immutable. In contrast to the method, `Equals()`, there is no default implementation of the equality operators, `==` and `!=`. If your `struct` needs them then you will have to implement them. -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. +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. 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. 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.