-
Notifications
You must be signed in to change notification settings - Fork 6k
👓 sweep docset for areas where in
wasn't mentioned but should be
#4534
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
BillWagner
merged 8 commits into
dotnet:master
from
BillWagner:in-parameter-tie-breaker
Mar 13, 2018
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f040558
sweep docset for areas where `in` wasn't mentioned but should be
BillWagner 9a96f57
respond to first round of feedback
BillWagner d9603bc
create in-modifer article
BillWagner 790f2c6
fix build errors
BillWagner fb153fa
fix one more build error
BillWagner 40b3773
wording change based on feedback.
BillWagner 2b69168
respond to edits
BillWagner 06cb128
commit changes from that unsaved window
BillWagner 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
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
68 changes: 68 additions & 0 deletions
68
docs/csharp/language-reference/keywords/in-parameter-modifier.md
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,68 @@ | ||
--- | ||
title: "in parameter modifier (C# Reference)" | ||
ms.date: 03/06/2018 | ||
ms.prod: .net | ||
ms.technology: | ||
- "devlang-csharp" | ||
ms.topic: "article" | ||
helpviewer_keywords: | ||
- "parameters [C#], in" | ||
- "in parameters [C#]" | ||
author: "BillWagner" | ||
ms.author: "wiwagn" | ||
--- | ||
|
||
# in parameter modifier (C# Reference) | ||
|
||
The `in` keyword causes arguments to be passed by reference. It is like the [ref](ref.md) or [out](out-parameter-modifier.md) keywords, except that `in` arguments cannot be modified by the called method, whereas `ref` arguments may be modified, `out` arguments must be modified by the caller, and those modifications are observable in the calling context. | ||
|
||
[!code-csharp-interactive[cs-in-keyword](../../../../samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/InParameterModifier.cs#1)] | ||
|
||
The preceding example demonstrates that the `in` modifier is unnecessary at the call site. It is only required in the method declaration. | ||
|
||
> [!NOTE] | ||
> The `in` keyword can also be used with a generic type parameter to specify that the type parameter is contravariant, as part of a `foreach` statement, or as part of a `join` clause in a LINQ query. For more information on the use of the `in` keyword in these contexts, see [in](in.md) which provides links to all those uses. | ||
|
||
Variables passed as `in` arguments must be initialized before being passed in a method call. However, the called method may not assign a value or modify the argument. | ||
|
||
Although the `in`, `ref`, and `out` keywords cause different run-time behavior, they are not considered part of the method signature at compile time. Therefore, methods cannot be overloaded if the only difference is that one method takes a `ref` or `in` argument and the other takes an `out` argument. The following code, for example, will not compile: | ||
|
||
```csharp | ||
class CS0663_Example | ||
{ | ||
// Compiler error CS0663: "Cannot define overloaded | ||
// methods that differ only on in, ref and out". | ||
public void SampleMethod(in int i) { } | ||
public void SampleMethod(ref int i) { } | ||
} | ||
``` | ||
|
||
Overloading based on the presence of `in` is allowed, but generates a compiler warning: | ||
|
||
```csharp | ||
class InOverloads | ||
{ | ||
// Discouraged. Calling SampleMethod(value) is ambiguous. | ||
public void SampleMethod(in int i) { } | ||
public void SampleMethod(int i) { } | ||
} | ||
``` | ||
|
||
Properties or constants may be passed as `in` parameters, because the calling method may not modify their values. | ||
|
||
You can't use the `in`, `ref`, and `out` keywords for the following kinds of methods: | ||
|
||
- Async methods, which you define by using the [async](../../../csharp/language-reference/keywords/async.md) modifier. | ||
|
||
- Iterator methods, which include a [yield return](../../../csharp/language-reference/keywords/yield.md) or `yield break` statement. | ||
|
||
You typically declare `in` arguments to avoid the copy operations necessary for passing arguments by value. This is most useful when arguments are structures or arrays of structures. | ||
|
||
## C# Language Specification | ||
[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] | ||
|
||
## See Also | ||
[C# Reference](../../../csharp/language-reference/index.md) | ||
[C# Programming Guide](../../../csharp/programming-guide/index.md) | ||
[C# Keywords](../../../csharp/language-reference/keywords/index.md) | ||
[Method Parameters](../../../csharp/language-reference/keywords/method-parameters.md) |
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
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.
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.
Modifying this table has to have been a nightmare.