Skip to content

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

Merged
merged 7 commits into from
Nov 15, 2017
Merged

Ref updates for 7.2 #3667

merged 7 commits into from
Nov 15, 2017

Conversation

BillWagner
Copy link
Member

Fixes #3241

Explain the concepts behind in parameters, ref readonly returns, readonly struct types and ref struct types.

@BillWagner BillWagner changed the title [WIP] Ref updates for 7.2 Ref updates for 7.2 Nov 14, 2017
@BillWagner
Copy link
Member Author

BillWagner commented Nov 14, 2017

I checked the build and the links, this is ready to review.

/cc @VSadov @jcouv

Copy link
Contributor

@rpetrusha rpetrusha left a 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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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

Copy link
Member Author

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
Copy link
Contributor

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.

Copy link
Member Author

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
Copy link
Contributor

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
Copy link
Contributor

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?

Copy link
Member Author

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
Copy link
Contributor

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
Copy link
Contributor

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.
@BillWagner
Copy link
Member Author

@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.
Copy link
Member

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".

Copy link
Member Author

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

Copy link
Contributor

@rpetrusha rpetrusha left a 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.

@BillWagner BillWagner merged commit 58c03e5 into dotnet:master Nov 15, 2017
@BillWagner BillWagner deleted the ref-updates-for-7-2 branch November 16, 2017 18:50
JRAlexander pushed a commit to JRAlexander/docs that referenced this pull request Nov 20, 2017
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants