-
Notifications
You must be signed in to change notification settings - Fork 6k
Add documentation for object initializer syntax without 'new' keyword #47036
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
+114
−2
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
231dc45
Initial plan
Copilot d5e5ee8
Add documentation for object initializer syntax without 'new' keyword
Copilot 6128432
Replace inline code example with snippet reference for object initial…
Copilot 7198ac4
Update object initializer examples to use target-typed new expressions
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...lasses-and-structs/snippets/object-collection-initializers/ObjectInitializerWithoutNew.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
namespace object_collection_initializers; | ||
|
||
// <SnippetObjectInitializerWithoutNew> | ||
public class ObjectInitializerWithoutNew | ||
{ | ||
public class Address | ||
{ | ||
public string Street { get; set; } = ""; | ||
public string City { get; set; } = ""; | ||
public string State { get; set; } = ""; | ||
} | ||
|
||
public class Person | ||
{ | ||
public string Name { get; set; } = ""; | ||
public Address HomeAddress { get; set; } = new Address(); // Property with setter | ||
BillWagner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public static void Examples() | ||
{ | ||
// Example 1: Using object initializer WITHOUT 'new' keyword | ||
// This modifies the existing Address instance created in the constructor | ||
var person1 = new Person | ||
{ | ||
Name = "Alice", | ||
HomeAddress = { Street = "123 Main St", City = "Anytown", State = "CA" } | ||
}; | ||
|
||
// Example 2: Using object initializer WITH 'new' keyword | ||
// This creates a completely new Address instance | ||
var person2 = new Person | ||
{ | ||
Name = "Bob", | ||
HomeAddress = new Address { Street = "456 Oak Ave", City = "Somewhere", State = "NY" } | ||
}; | ||
|
||
// Both approaches work, but they behave differently: | ||
// - person1.HomeAddress is the same instance that was created in Person's constructor | ||
// - person2.HomeAddress is a new instance, replacing the one from the constructor | ||
|
||
Console.WriteLine($"Person 1: {person1.Name} at {person1.HomeAddress.Street}, {person1.HomeAddress.City}, {person1.HomeAddress.State}"); | ||
Console.WriteLine($"Person 2: {person2.Name} at {person2.HomeAddress.Street}, {person2.HomeAddress.City}, {person2.HomeAddress.State}"); | ||
} | ||
} | ||
// </SnippetObjectInitializerWithoutNew> | ||
|
||
// <SnippetReadOnlyPropertyExample> | ||
public class ReadOnlyPropertyExample | ||
{ | ||
public class Settings | ||
{ | ||
public string Theme { get; set; } = "Light"; | ||
public int FontSize { get; set; } = 12; | ||
} | ||
|
||
public class Application | ||
{ | ||
public string Name { get; set; } = ""; | ||
// This property is read-only - it can only be set during construction | ||
public Settings AppSettings { get; } = new Settings(); | ||
BillWagner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public static void Example() | ||
{ | ||
// You can still initialize the nested object's properties | ||
// even though AppSettings property has no setter | ||
var app = new Application | ||
{ | ||
Name = "MyApp", | ||
AppSettings = { Theme = "Dark", FontSize = 14 } | ||
}; | ||
|
||
// This would cause a compile error because AppSettings has no setter: | ||
// app.AppSettings = new Settings { Theme = "Dark", FontSize = 14 }; | ||
|
||
Console.WriteLine($"App: {app.Name}, Theme: {app.AppSettings.Theme}, Font Size: {app.AppSettings.FontSize}"); | ||
} | ||
} | ||
// </SnippetReadOnlyPropertyExample> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.