Skip to content

Enhance/correct some examples in structs.md #637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions standard/structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ With classes, it is possible for two variables to reference the same object, and
<!-- markdownlint-disable MD028 -->

<!-- markdownlint-enable MD028 -->
> *Example*: Given the declaration
> *Example*: Given the following
>
> ```csharp
> struct Point
Expand All @@ -177,18 +177,20 @@ With classes, it is possible for two variables to reference the same object, and
> this.y = y;
> }
> }
> ```
>
> the code fragment
>
> ```csharp
> Point a = new Point(10, 10);
> Point b = a;
> a.x = 100;
> System.Console.WriteLine(b.x);
> ```
> class A
> {
> static void Main()
> {
> Point a = new Point(10, 10);
> Point b = a;
> a.x = 100;
> System.Console.WriteLine(b.x);
> }
> }>
> ```
>
> outputs the value `10`. The assignment of `a` to `b` creates a copy of the value, and `b` is thus unaffected by the assignment to `a.x`. Had `Point` instead been declared as a class, the output would be `100` because `a` and `b` would reference the same object.
> the output is `10`. The assignment of `a` to `b` creates a copy of the value, and `b` is thus unaffected by the assignment to `a.x`. Had `Point` instead been declared as a class, the output would be `100` because `a` and `b` would reference the same object.
>
> *end example*

Expand Down Expand Up @@ -287,7 +289,7 @@ The meaning of `this` in a struct differs from the meaning of `this` in a class,
> T x = new T();
> Console.WriteLine(x.ToString());
> Console.WriteLine(x.ToString());
> console.WriteLine(x.ToString());
> Console.WriteLine(x.ToString());
> }
>
> static void Main() => Test<Counter>();
Expand Down Expand Up @@ -375,7 +377,7 @@ As described in [§15.4.5](structs.md#1545-default-values), the default value of

Unlike a class, a struct is not permitted to declare a parameterless instance constructor. Instead, every struct implicitly has a parameterless instance constructor, which always returns the value that results from setting all value type fields to their default value and all reference type fields to `null` ([§8.3.3](types.md#833-default-constructors)). A struct can declare instance constructors having parameters.

> *Example*:
> *Example*: Given the following
>
> ```csharp
> struct Point
Expand All @@ -388,16 +390,18 @@ Unlike a class, a struct is not permitted to declare a parameterless instance co
> this.y = y;
> }
> }
> ```
>
> Given the above declaration, the statements
>
> ```csharp
> Point p1 = new Point();
> Point p2 = new Point(0, 0);
> class A
> {
> static void Main()
> {
> Point p1 = new Point();
> Point p2 = new Point(0, 0);
> }
> }
> ```
>
> both create a `Point` with `x` and `y` initialized to zero.
> the statements both create a `Point` with `x` and `y` initialized to zero.
>
> *end example*

Expand Down