Skip to content

Commit 5941f1e

Browse files
Merge pull request #53189 from ChrisHowd/main
updated sample code in Manage Class Implementations module
2 parents e1a43ad + 7c2619a commit 5941f1e

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

learn-pr/wwl-azure/manage-class-implementations/includes/6-instantiate-objects-initializers-copy-constructor.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,6 @@ public class HowToIndexInitializer
218218
You use the required keyword to force callers to set the value of a property or field using an object initializer. Required properties don't need to be set as constructor parameters. The compiler ensures all callers initialize those values.
219219

220220
```csharp
221-
222-
public class Person
223-
{
224-
public string FirstName { get; set; } = string.Empty;
225-
public required string LastName { get; set; }
226-
227-
}
228-
229221
// The `FirstName` property is optional and has a default value of an empty string.
230222
// The `LastName` property is required and must be initialized during object creation.
231223
// You can create a new instance of the Person class using both properties.
@@ -247,9 +239,16 @@ Console.WriteLine($"Hello, {friend2.FirstName} {friend2.LastName}!");
247239
248240
// Output:
249241
// Hello, Lisa Yeh!
250-
// Hello, Sandy Yeh!
251242
// Hello, Yeh!
252-
// Hello, Chen!
243+
// Hello, Sandy Chen!
244+
245+
public class Person
246+
{
247+
public string FirstName { get; set; } = string.Empty;
248+
public required string LastName { get; set; }
249+
250+
}
251+
253252
```
254253

255254
It's a typical practice to guarantee that your object is properly initialized, especially when you have multiple fields or properties to manage and don't want to include them all in the constructor.

0 commit comments

Comments
 (0)