Skip to content

Conversation

@tdykstra
Copy link
Contributor

@tdykstra tdykstra commented Mar 2, 2021

Fixes #20583

title: "init keyword - C# Reference"
ms.date: 03/10/2017
f1_keywords:
- "init"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidwengier Do we need this? or only init_CSharpKeyword is sufficient?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly don't need it from Roslyn's point of view.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BillWagner The articles in the keywords folder have two entries under f1_keywords (sometimes in reverse order):

f1_keywords: 
  - "<keyword>_CSharpKeyword"
  - "<keyword>"

Is the second entry superfluous?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the second entry is superfluous. Ping @ghogen to verify.

@@ -0,0 +1,11 @@

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding all these new snippets under a new directory named init.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. That would indeed be the prescribed pattern, but the snippets folder at present consistently follows the pattern of locating all snippets for the keywords folder directly under keywords/snippets, without separating snippets into subfolders named after articles. Rather than introduce an inconsistency here I think it better to follow the existing pattern.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @tdykstra We agreed that both meet the standard. This is especially true in the language reference area for keywords and operators.

@tdykstra
Copy link
Contributor Author

tdykstra commented Mar 3, 2021

Thanks for the review and corrections, @Youssef1313 !

@tdykstra tdykstra marked this pull request as ready for review March 3, 2021 18:16
@tdykstra tdykstra requested a review from BillWagner as a code owner March 3, 2021 18:16
Base automatically changed from master to main March 5, 2021 23:33
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all LGTM @tdykstra

I had a couple minor suggestions, and then this is ready to :shipit:

The class that is shown in the previous example is mutable. Client code can change the values in objects after creation. In complex classes that contain significant behavior (methods) as well as data, it's often necessary to have public properties. However, for small classes or structs that just encapsulate a set of values (data) and have little or no behaviors, you should either make the objects immutable by declaring the set accessor as [private](../../language-reference/keywords/private.md) (immutable to consumers) or by declaring only a get accessor (immutable everywhere except the constructor). For more information, see [How to implement a lightweight class with auto-implemented properties](./how-to-implement-a-lightweight-class-with-auto-implemented-properties.md).
The class that is shown in the previous example is mutable. Client code can change the values in objects after creation. In complex classes that contain significant behavior (methods) as well as data, it's often necessary to have public properties. However, for small classes or structs that just encapsulate a set of values (data) and have little or no behaviors, you should use one of the following options for making the objects immutable:

* Declare the `set` accessor as [private](../../language-reference/keywords/private.md) (immutable to consumers).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move this to the bottom of the list, making it the least good option.

Comment on lines 18 to 20
- Declare the [set](../../language-reference/keywords/set.md) accessor to be [private](../../language-reference/keywords/private.md). The property is settable within the type, but it is immutable to consumers.

When you declare a private `set` accessor, you cannot use an object initializer to initialize the property. You must use a constructor or a factory method.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here again, I'd move these two paragraphs below the recommendation to "Declare only the get accessor, ..."

@tdykstra tdykstra merged commit 2f692b3 into dotnet:main Mar 10, 2021
@kyoyama-kazusa
Copy link

kyoyama-kazusa commented Mar 20, 2021

:D

I found the new page to introduce the keyword init has added into the docs, but the keyword list page doesn't display this keyword.

Could you please add this keyword into the list page? Thank you!

@Youssef1313
Copy link
Member

@SunnieShine This is tracked by #21603.

@kyoyama-kazusa
Copy link

@Youssef1313 Oh! I only found that init page has been added, but no displaying in that list page.

I don't know there's an issue to describe that.

Thank you for your mention!

@tdykstra tdykstra mentioned this pull request Mar 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Language reference: init only setters

6 participants