-
Notifications
You must be signed in to change notification settings - Fork 6k
Ref updates for 7.2 #3667
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
Ref updates for 7.2 #3667
Conversation
Ready for a test build.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good. Left a number of comments, @BillWagner.
An advantage to using value types is that they often avoid heap allocations. | ||
The corresponding disadvantage is that they are copied by value. This tradeoff | ||
makes it harder to optimize algorithms that operate on large amounts of | ||
data. New language features in C# 7.2 provide mechanisms that enable pass-by-refernce |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reference is misspelled here.
The corresponding disadvantage is that they are copied by value. This tradeoff | ||
makes it harder to optimize algorithms that operate on large amounts of | ||
data. New language features in C# 7.2 provide mechanisms that enable pass-by-refernce | ||
semantics with value types. Use these features wisely, and you can minimize both allocations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very minor nit: Use these features wisely, and you --> If you use these features wisely, you
and copy operations. This article explores those new features. | ||
|
||
Much of the sample code in this article demonstrates features added in C# 7.2. In order to | ||
use those features, you have to configure the compiler to use C# 7.2 or later in your |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you configure the project, not the compiler: ...configure your project to use the C# 7.2 or later compiler.
use those features, you have to configure the compiler to use C# 7.2 or later in your | ||
project. You can use Visual Studio to select it. For each project, select **Project** from | ||
the menu, then **Properties**. Select the **Build** tab and click **Advanced**. From there, | ||
you can configure the language version. Choose either "7.2", or "latest". Or, you can |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no comma after Or
When you add the `in` modify to pass an argument by reference, you declare | ||
your design intent is to pass arguments by reference to | ||
avoid unnecessary copying. You do not intend to modify the object used | ||
as that argument. The following code shows an example of a method |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A little bit more context might be valuable here: value types when passed as parameters (either by reference or by value) are ordinarily copied
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I added more context in the paragraph above.
returning the object. | ||
|
||
Finally, the compiler generates more efficient code when you call members of a | ||
`readonly struct`: The `this` reference is always an `in` parameter, instead |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hard to parse. Perhaps "The this
reference, instead of a copy of the receiver, is always an in
parameter passed by value to the member method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, it should be "passed by reference" not "passed by value".
Finally, the compiler generates more efficient code when you call members of a | ||
`readonly struct`: The `this` reference is always an `in` parameter, instead | ||
of a copy of the receiver, passed by value to the member method. This optimization | ||
saves more copying when you use a `readonly struct`. The `Point3D` would be a great |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: would be --> is
must be stack allocated. In other words, these types can never be created on the | ||
heap as a member of another class. The primary motivation for this feature | ||
was `Span<T>` and related structures, but you can create `ref struct` types | ||
when you have similar requirements. In this article, you see examples |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The requirements aren't clear. Can you mention a use case here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I re-wrote that section. Likely, using Span<T>
and related are the most common use case. I tried to make that point, and point to other uses.
types declared as `ref struct` include `ReadOnlySpan<T>`, `Memory<T>`, and | ||
`ReadOnlyMemory<T>`. | ||
|
||
The goal of keeping a `ref struct` type as a stack allocated variable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stack allocated --> stack-allocated
algorithms where memory allocations can be critical to achieving the | ||
necessary performance. You may find that you don't often use these features in | ||
the code you write. However, these enhancements have been adopted in many locations | ||
in the .NET framework. As more and more APIs make use of these features, you'll |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
framework --> Framework
"reference-semantics-with-value-types.md" better reflects the content.
@rpetrusha I updated everything based on your comments. Let me know if this is ready. I also changed the file name in a separate commit so it should be easier to review. |
</PropertyGroup> | ||
``` | ||
|
||
You can use either "7.2" or "latest" for the value. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that there are multiple ways of updating a project (UI settings, editing csproj, code fix), consider factoring this section to a dedicated page, and referenced from here.
Then you can just have one line: "In order to use those features, you have to configure your project to use C# 7.2 or later in your project, by setting the language version to "7.2" or "latest" per these instructions".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd agree.
I want to take a bit more time to do that, so I made an issue that can get addressed in this or the next sprint: #3685
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, @BillWagner. This is good to merge when you're ready, after the build completes.
* first draft ready * add samples Ready for a test build. * give it a proofread * Fix the build errors. * respoond to feedback * rename file "reference-semantics-with-value-types.md" better reflects the content. * fix a build error
Fixes #3241
Explain the concepts behind
in
parameters,ref readonly
returns,readonly struct
types andref struct
types.