Skip to content

Commit 97b45d8

Browse files
authored
Enhance/correct some examples in structs.md (dotnet#637)
1 parent a6a4cd5 commit 97b45d8

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

standard/structs.md

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ With classes, it is possible for two variables to reference the same object, and
164164
<!-- markdownlint-disable MD028 -->
165165
166166
<!-- markdownlint-enable MD028 -->
167-
> *Example*: Given the declaration
167+
> *Example*: Given the following
168168
>
169169
> ```csharp
170170
> struct Point
@@ -177,18 +177,20 @@ With classes, it is possible for two variables to reference the same object, and
177177
> this.y = y;
178178
> }
179179
> }
180-
> ```
181-
>
182-
> the code fragment
183180
>
184-
> ```csharp
185-
> Point a = new Point(10, 10);
186-
> Point b = a;
187-
> a.x = 100;
188-
> System.Console.WriteLine(b.x);
189-
> ```
181+
> class A
182+
> {
183+
> static void Main()
184+
> {
185+
> Point a = new Point(10, 10);
186+
> Point b = a;
187+
> a.x = 100;
188+
> System.Console.WriteLine(b.x);
189+
> }
190+
> }>
191+
> ```
190192
>
191-
> 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.
193+
> 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.
192194
>
193195
> *end example*
194196
@@ -287,7 +289,7 @@ The meaning of `this` in a struct differs from the meaning of `this` in a class,
287289
> T x = new T();
288290
> Console.WriteLine(x.ToString());
289291
> Console.WriteLine(x.ToString());
290-
> console.WriteLine(x.ToString());
292+
> Console.WriteLine(x.ToString());
291293
> }
292294
>
293295
> static void Main() => Test<Counter>();
@@ -375,7 +377,7 @@ As described in [§15.4.5](structs.md#1545-default-values), the default value of
375377
376378
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.
377379
378-
> *Example*:
380+
> *Example*: Given the following
379381
>
380382
> ```csharp
381383
> struct Point
@@ -388,16 +390,18 @@ Unlike a class, a struct is not permitted to declare a parameterless instance co
388390
> this.y = y;
389391
> }
390392
> }
391-
> ```
392393
>
393-
> Given the above declaration, the statements
394-
>
395-
> ```csharp
396-
> Point p1 = new Point();
397-
> Point p2 = new Point(0, 0);
394+
> class A
395+
> {
396+
> static void Main()
397+
> {
398+
> Point p1 = new Point();
399+
> Point p2 = new Point(0, 0);
400+
> }
401+
> }
398402
> ```
399403
>
400-
> both create a `Point` with `x` and `y` initialized to zero.
404+
> the statements both create a `Point` with `x` and `y` initialized to zero.
401405
>
402406
> *end example*
403407

0 commit comments

Comments
 (0)