diff --git a/docs/csharp/lambda-expressions.md b/docs/csharp/lambda-expressions.md index ef00dcb29f09f..c097fc55e792b 100644 --- a/docs/csharp/lambda-expressions.md +++ b/docs/csharp/lambda-expressions.md @@ -146,7 +146,7 @@ Lambdas can refer to *outer variables* (see [Anonymous methods](programming-guid - Variables introduced within a lambda expression are not visible in the outer method. -- A lambda expression cannot directly capture a `ref` or `out` parameter from an enclosing method. +- A lambda expression cannot directly capture an `in`, `ref`, or `out` parameter from an enclosing method. - A return statement in a lambda expression does not cause the enclosing method to return. diff --git a/docs/csharp/language-reference/keywords/async.md b/docs/csharp/language-reference/keywords/async.md index aab45897c943e..3af33ba6e642e 100644 --- a/docs/csharp/language-reference/keywords/async.md +++ b/docs/csharp/language-reference/keywords/async.md @@ -73,7 +73,7 @@ An async method can have the following return types: - [void](../../../csharp/language-reference/keywords/void.md), which should only be used for event handlers. - Starting with C# 7, any type that has an accessible `GetAwaiter` method. The `System.Threading.Tasks.ValueTask` type is one such implementation. It is available by adding the NuGet package `System.Threading.Tasks.Extensions`. -The async method can't declare any [ref](../../../csharp/language-reference/keywords/ref.md) or [out](../../../csharp/language-reference/keywords/out.md) parameters, nor can it have a reference return value, but it can call methods that have such parameters. +The async method can't declare any [in](../../../csharp/language-reference/keywords/in-parameter-modifier.md), [ref](../../../csharp/language-reference/keywords/ref.md) or [out](../../../csharp/language-reference/keywords/out-parameter-modifier.md) parameters, nor can it have a [reference return value](../../programming-guide/classes-and-structs/ref-returns.md), but it can call methods that have such parameters. You specify `Task` as the return type of an async method if the [return](../../../csharp/language-reference/keywords/return.md) statement of the method specifies an operand of type `TResult`. You use `Task` if no meaningful value is returned when the method is completed. That is, a call to the method returns a `Task`, but when the `Task` is completed, any `await` expression that's awaiting the `Task` evaluates to `void`. diff --git a/docs/csharp/language-reference/keywords/in-generic-modifier.md b/docs/csharp/language-reference/keywords/in-generic-modifier.md index 032bf224e3a74..880ad42a2ce39 100644 --- a/docs/csharp/language-reference/keywords/in-generic-modifier.md +++ b/docs/csharp/language-reference/keywords/in-generic-modifier.md @@ -8,30 +8,32 @@ ms.topic: "article" helpviewer_keywords: - "contravariance, in keyword [C#]" - "in keyword [C#]" -ms.assetid: 3a778c36-8aed-4ebe-aa8b-39f4057215b1 -caps.latest.revision: 17 author: "BillWagner" ms.author: "wiwagn" --- + # in (Generic Modifier) (C# Reference) + For generic type parameters, the `in` keyword specifies that the type parameter is contravariant. You can use the `in` keyword in generic interfaces and delegates. Contravariance enables you to use a less derived type than that specified by the generic parameter. This allows for implicit conversion of classes that implement variant interfaces and implicit conversion of delegate types. Covariance and contravariance in generic type parameters are supported for reference types, but they are not supported for value types. - A type can be declared contravariant in a generic interface or delegate if it is used only as a type of method arguments and not used as a method return type. `Ref` and `out` parameters cannot be variant. + A type can be declared contravariant in a generic interface or delegate only if it defines the type of a method's parameters and not of a method's return type. `In`, `ref`, and `out` parameters must be invariant, meaning they are neither covariant or contravariant. - An interface that has a contravariant type parameter allows its methods to accept arguments of less derived types than those specified by the interface type parameter. For example, because in .NET Framework 4, in the interface, type T is contravariant, you can assign an object of the `IComparer(Of Person)` type to an object of the `IComparer(Of Employee)` type without using any special conversion methods if `Employee` inherits `Person`. + An interface that has a contravariant type parameter allows its methods to accept arguments of less derived types than those specified by the interface type parameter. For example, in the interface, type T is contravariant, you can assign an object of the `IComparer` type to an object of the `IComparer` type without using any special conversion methods if `Employee` inherits `Person`. A contravariant delegate can be assigned another delegate of the same type, but with a less derived generic type parameter. For more information, see [Covariance and Contravariance](../../programming-guide/concepts/covariance-contravariance/index.md). -## Example +## Contravariant generic interface + The following example shows how to declare, extend, and implement a contravariant generic interface. It also shows how you can use implicit conversion for classes that implement this interface. [!code-csharp[csVarianceKeywords#1](../../../csharp/language-reference/keywords/codesnippet/CSharp/in-generic-modifier_1.cs)] -## Example +## Contravariant generic delegate + The following example shows how to declare, instantiate, and invoke a contravariant generic delegate. It also shows how you can implicitly convert a delegate type. [!code-csharp[csVarianceKeywords#2](../../../csharp/language-reference/keywords/codesnippet/CSharp/in-generic-modifier_2.cs)] @@ -42,4 +44,4 @@ For generic type parameters, the `in` keyword specifies that the type parameter ## See Also [out](../../../csharp/language-reference/keywords/out-generic-modifier.md) [Covariance and Contravariance](../../programming-guide/concepts/covariance-contravariance/index.md) - [Modifiers](../../../csharp/language-reference/keywords/modifiers.md) + [Modifiers](../../../csharp/language-reference/keywords/modifiers.md) diff --git a/docs/csharp/language-reference/keywords/in-parameter-modifier.md b/docs/csharp/language-reference/keywords/in-parameter-modifier.md new file mode 100644 index 0000000000000..23b2a3c5cd34a --- /dev/null +++ b/docs/csharp/language-reference/keywords/in-parameter-modifier.md @@ -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) diff --git a/docs/csharp/language-reference/keywords/in.md b/docs/csharp/language-reference/keywords/in.md index 670d10980b7c0..c43685ac975bb 100644 --- a/docs/csharp/language-reference/keywords/in.md +++ b/docs/csharp/language-reference/keywords/in.md @@ -1,6 +1,6 @@ --- title: "in (C# Reference)" -ms.date: 07/20/2015 +ms.date: 02/06/2018 ms.prod: .net ms.technology: - "devlang-csharp" @@ -10,19 +10,19 @@ f1_keywords: - "in_CSharpKeyword" helpviewer_keywords: - "in keyword [C#]" -ms.assetid: 52032838-0a38-476e-b4d5-94b59141952f -caps.latest.revision: 9 author: "BillWagner" ms.author: "wiwagn" --- + # in (C# Reference) -The `in` contextual keyword is used in three contexts: + +The `in` contextual keyword is used in four contexts: -- [foreach](../../../csharp/language-reference/keywords/foreach-in.md) statements - -- [join clauses](../../../csharp/language-reference/keywords/join-clause.md) in query expressions - -- [generic type parameters](../../../csharp/language-reference/keywords/in-generic-modifier.md) in generic interfaces and delegates. +- [generic type parameters](in-generic-modifier.md) in generic interfaces and delegates. +- As a [parameter modifier](in-parameter-modifier.md), which lets you pass an argument to a method by reference rather than by value. +- [foreach](foreach-in.md) statements. +- [join clauses](join-clause.md) in LINQ query expressions. ## See Also - [C# Reference](../../../csharp/language-reference/index.md) + [C# Keywords](index.md) + [C# Reference](../index.md) diff --git a/docs/csharp/language-reference/keywords/index.md b/docs/csharp/language-reference/keywords/index.md index 65c9439e933a9..b1e1effce317e 100644 --- a/docs/csharp/language-reference/keywords/index.md +++ b/docs/csharp/language-reference/keywords/index.md @@ -32,18 +32,18 @@ Keywords are predefined, reserved identifiers that have special meanings to the |[event](../../../csharp/language-reference/keywords/event.md)|[explicit](../../../csharp/language-reference/keywords/explicit.md)|[extern](../../../csharp/language-reference/keywords/extern.md)|[false](../../../csharp/language-reference/keywords/false.md)| |[finally](../../../csharp/language-reference/keywords/try-finally.md)|[fixed](../../../csharp/language-reference/keywords/fixed-statement.md)|[float](../../../csharp/language-reference/keywords/float.md)|[for](../../../csharp/language-reference/keywords/for.md)| |[foreach](../../../csharp/language-reference/keywords/foreach-in.md)|[goto](../../../csharp/language-reference/keywords/goto.md)|[if](../../../csharp/language-reference/keywords/if-else.md)|[implicit](../../../csharp/language-reference/keywords/implicit.md)| -|[in](../../../csharp/language-reference/keywords/foreach-in.md)|[in (generic modifier)](../../../csharp/language-reference/keywords/in-generic-modifier.md)|[int](../../../csharp/language-reference/keywords/int.md)|[interface](../../../csharp/language-reference/keywords/interface.md)| -|[internal](../../../csharp/language-reference/keywords/internal.md)|[is](../../../csharp/language-reference/keywords/is.md)|[lock](../../../csharp/language-reference/keywords/lock-statement.md)|[long](../../../csharp/language-reference/keywords/long.md)| -|[namespace](../../../csharp/language-reference/keywords/namespace.md)|[new](../../../csharp/language-reference/keywords/new.md)|[null](../../../csharp/language-reference/keywords/null.md)|[object](../../../csharp/language-reference/keywords/object.md)| -[operator](../../../csharp/language-reference/keywords/operator.md)|[out](../../../csharp/language-reference/keywords/out.md)|[out (generic modifier)](../../../csharp/language-reference/keywords/out-generic-modifier.md)|[override](../../../csharp/language-reference/keywords/override.md)| -|[params](../../../csharp/language-reference/keywords/params.md)|[private](../../../csharp/language-reference/keywords/private.md)|[protected](../../../csharp/language-reference/keywords/protected.md)|[public](../../../csharp/language-reference/keywords/public.md)| -|[readonly](../../../csharp/language-reference/keywords/readonly.md)|[ref](../../../csharp/language-reference/keywords/ref.md)|[return](../../../csharp/language-reference/keywords/return.md)|[sbyte](../../../csharp/language-reference/keywords/sbyte.md)| -|[sealed](../../../csharp/language-reference/keywords/sealed.md)|[short](../../../csharp/language-reference/keywords/short.md)|[sizeof](../../../csharp/language-reference/keywords/sizeof.md)|[stackalloc](../../../csharp/language-reference/keywords/stackalloc.md)| -|[static](../../../csharp/language-reference/keywords/static.md)|[string](../../../csharp/language-reference/keywords/string.md)|[struct](../../../csharp/language-reference/keywords/struct.md)|[switch](../../../csharp/language-reference/keywords/switch.md)| -|[this](../../../csharp/language-reference/keywords/this.md)|[throw](../../../csharp/language-reference/keywords/throw.md)|[true](../../../csharp/language-reference/keywords/true.md)|[try](../../../csharp/language-reference/keywords/try-catch.md)| -|[typeof](../../../csharp/language-reference/keywords/typeof.md)|[uint](../../../csharp/language-reference/keywords/uint.md)|[ulong](../../../csharp/language-reference/keywords/ulong.md)|[unchecked](../../../csharp/language-reference/keywords/unchecked.md)| -|[unsafe](../../../csharp/language-reference/keywords/unsafe.md)|[ushort](../../../csharp/language-reference/keywords/ushort.md)|[using](../../../csharp/language-reference/keywords/using.md)|[using static](using-static.md)| -|[virtual](../../../csharp/language-reference/keywords/virtual.md)|[void](../../../csharp/language-reference/keywords/void.md)|[volatile](../../../csharp/language-reference/keywords/volatile.md)|[while](../../../csharp/language-reference/keywords/while.md)| +|[in](../../../csharp/language-reference/keywords/in.md)|[int](../../../csharp/language-reference/keywords/int.md)|[interface](../../../csharp/language-reference/keywords/interface.md)|[internal](../../../csharp/language-reference/keywords/internal.md)| +|[is](../../../csharp/language-reference/keywords/is.md)|[lock](../../../csharp/language-reference/keywords/lock-statement.md)|[long](../../../csharp/language-reference/keywords/long.md)|[namespace](../../../csharp/language-reference/keywords/namespace.md)| +|[new](../../../csharp/language-reference/keywords/new.md)|[null](../../../csharp/language-reference/keywords/null.md)|[object](../../../csharp/language-reference/keywords/object.md)|[operator](../../../csharp/language-reference/keywords/operator.md)| +|[out](../../../csharp/language-reference/keywords/out.md)|[override](../../../csharp/language-reference/keywords/override.md)|[params](../../../csharp/language-reference/keywords/params.md)|[private](../../../csharp/language-reference/keywords/private.md)| +|[protected](../../../csharp/language-reference/keywords/protected.md)|[public](../../../csharp/language-reference/keywords/public.md)|[readonly](../../../csharp/language-reference/keywords/readonly.md)|[ref](../../../csharp/language-reference/keywords/ref.md)| +|[return](../../../csharp/language-reference/keywords/return.md)|[sbyte](../../../csharp/language-reference/keywords/sbyte.md)|[sealed](../../../csharp/language-reference/keywords/sealed.md)|[short](../../../csharp/language-reference/keywords/short.md)|| +[sizeof](../../../csharp/language-reference/keywords/sizeof.md)|[stackalloc](../../../csharp/language-reference/keywords/stackalloc.md)|[static](../../../csharp/language-reference/keywords/static.md)|[string](../../../csharp/language-reference/keywords/string.md)| +|[struct](../../../csharp/language-reference/keywords/struct.md)|[switch](../../../csharp/language-reference/keywords/switch.md)|[this](../../../csharp/language-reference/keywords/this.md)|[throw](../../../csharp/language-reference/keywords/throw.md)| +|[true](../../../csharp/language-reference/keywords/true.md)|[try](../../../csharp/language-reference/keywords/try-catch.md)|[typeof](../../../csharp/language-reference/keywords/typeof.md)|[uint](../../../csharp/language-reference/keywords/uint.md)| +|[ulong](../../../csharp/language-reference/keywords/ulong.md)|[unchecked](../../../csharp/language-reference/keywords/unchecked.md)|[unsafe](../../../csharp/language-reference/keywords/unsafe.md)|[ushort](../../../csharp/language-reference/keywords/ushort.md)| +|[using](../../../csharp/language-reference/keywords/using.md)|[using static](using-static.md)|[virtual](../../../csharp/language-reference/keywords/virtual.md)|[void](../../../csharp/language-reference/keywords/void.md)| +|[volatile](../../../csharp/language-reference/keywords/volatile.md)|[while](../../../csharp/language-reference/keywords/while.md)| ## Contextual Keywords A contextual keyword is used to provide a specific meaning in the code, but it is not a reserved word in C#. Some contextual keywords, such as `partial` and `where`, have special meanings in two or more contexts. diff --git a/docs/csharp/language-reference/keywords/method-parameters.md b/docs/csharp/language-reference/keywords/method-parameters.md index fb558ed24f39c..e3a58e61eb87b 100644 --- a/docs/csharp/language-reference/keywords/method-parameters.md +++ b/docs/csharp/language-reference/keywords/method-parameters.md @@ -15,15 +15,18 @@ author: "BillWagner" ms.author: "wiwagn" --- # Method Parameters (C# Reference) -If a parameter is declared for a method without [ref](../../../csharp/language-reference/keywords/ref.md) or [out](../../../csharp/language-reference/keywords/out.md), the parameter can have a value associated with it. That value can be changed in the method, but the changed value will not be retained when control passes back to the calling procedure. By using a method parameter keyword, you can change this behavior. + +Parameters declared for a method without [in](../../../csharp/language-reference/keywords/in-parameter-modifier.md), [ref](../../../csharp/language-reference/keywords/ref.md) or [out](../../../csharp/language-reference/keywords/out-parameter-modifier.md), are passed to the called method by value. That value can be changed in the method, but the changed value will not be retained when control passes back to the calling procedure. By using a method parameter keyword, you can change this behavior. This section describes the keywords you can use when declaring method parameters: - [params](../../../csharp/language-reference/keywords/params.md) +- [in](../../../csharp/language-reference/keywords/in-parameter-modifier.md) + - [ref](../../../csharp/language-reference/keywords/ref.md) -- [out](../../../csharp/language-reference/keywords/out.md) +- [out](../../../csharp/language-reference/keywords/out-parameter-modifier.md) ## See Also [C# Reference](../../../csharp/language-reference/index.md) diff --git a/docs/csharp/language-reference/keywords/out-parameter-modifier.md b/docs/csharp/language-reference/keywords/out-parameter-modifier.md index 61f5bb4d2a35d..d67771a061c8f 100644 --- a/docs/csharp/language-reference/keywords/out-parameter-modifier.md +++ b/docs/csharp/language-reference/keywords/out-parameter-modifier.md @@ -1,6 +1,6 @@ --- title: "out parameter modifier (C# Reference)" -ms.date: 07/20/2015 +ms.date: 03/06/2018 ms.prod: .net ms.technology: - "devlang-csharp" @@ -8,22 +8,20 @@ ms.topic: "article" helpviewer_keywords: - "parameters [C#], out" - "out parameters [C#]" -ms.assetid: 3fce0dc5-03f4-4faa-bd61-36c41bc6baf1 -caps.latest.revision: 9 author: "BillWagner" ms.author: "wiwagn" --- # out parameter modifier (C# Reference) -The `out` keyword causes arguments to be passed by reference. It is like the [ref](../../../csharp/language-reference/keywords/ref.md) keyword, except that `ref` requires that the variable be initialized before it is passed. To use an `out` parameter, both the method definition and the calling method must explicitly use the `out` keyword. For example: +The `out` keyword causes arguments to be passed by reference. It is like the [ref](ref.md) keyword, except that `ref` requires that the variable be initialized before it is passed. It is also like the [in](in-parameter-modifier.md) keyword, except that `in` does not allow the called method to modify the argument value. To use an `out` parameter, both the method definition and the calling method must explicitly use the `out` keyword. For example: - [!code-csharp[cs-out-keyword](../../../../samples/snippets/csharp/language-reference/keywords/out/out-1.cs)] +[!code-csharp-interactive[cs-out-keyword](../../../../samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/OutParameterModifier.cs#1)] > [!NOTE] -> The `out` keyword can also be used with a generic type parameter to specify that the type parameter is covariant. For more information on the use of the `out` keyword in this context, see [out (Generic Modifier)](../../../csharp/language-reference/keywords/out-generic-modifier.md). +> The `out` keyword can also be used with a generic type parameter to specify that the type parameter is covariant. For more information on the use of the `out` keyword in this context, see [out (Generic Modifier)](out-generic-modifier.md). - Variables passed as `out` arguments do not have to be initialized before being passed in a method call. However, the called method is required to assign a value before the method returns. +Variables passed as `out` arguments do not have to be initialized before being passed in a method call. However, the called method is required to assign a value before the method returns. - Although the `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` argument and the other takes an `out` argument. The following code, for example, will not compile: +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 @@ -35,15 +33,17 @@ class CS0663_Example } ``` -Overloading is legal, however, if one method takes a `ref` or `out` argument and the other uses neither, like this: +Overloading is legal, however, if one method takes a `ref`, `in`, or `out` argument and the other has none of those modifiers, like this: - [!code-csharp[csrefKeywordsMethodParams#3](../../../../samples/snippets/csharp/language-reference/keywords/out/out-3.cs)] - - Properties are not variables and therefore cannot be passed as `out` parameters. +[!code-csharp[cs-out-keyword](../../../../samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/OutParameterModifier.cs#2)] + +The compiler chooses the best overload by matching the parameter modifiers at the call site to the parameter modifiers used in the method call. + +Properties are not variables and therefore cannot be passed as `out` parameters. For information about passing arrays, see [Passing Arrays Using ref and out](../../../csharp/programming-guide/arrays/passing-arrays-using-ref-and-out.md). - You can't use the `ref` and `out` keywords for the following kinds of methods: + 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. @@ -53,7 +53,7 @@ Overloading is legal, however, if one method takes a `ref` or `out` argument and Declaring a method with `out` arguments is useful when you want a method to return multiple values. The following example uses `out` to return three variables with a single method call. Note that the third argument is assigned to null. This enables methods to return values optionally. - [!code-csharp[csrefKeywordsMethodParams#4](../../../../samples/snippets/csharp/language-reference/keywords/out/out-4.cs)] +[!code-csharp-interactive[cs-out-keyword](../../../../samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/OutParameterModifier.cs#3)] The [Try pattern](/visualstudio/code-quality/ca1021-avoid-out-parameters#try-pattern-methods.md) involves returning a `bool` to indicate whether an operation succeeded and failed, and returning the value produced by the operation in an `out` argument. A number of parsing methods, such as the [DateTime.TryParse](xref:System.DateTime.TryParse(System.String,System.DateTime@)) method, use this pattern. @@ -61,18 +61,18 @@ Overloading is legal, however, if one method takes a `ref` or `out` argument and In C# 6 and earlier, you must declare a variable in a separate statement before you pass it as an `out` argument. The following example declares a variable named `number` before it is passed to the [Int32.TryParse](xref:System.Int32.TryParse(System.String,System.Int32@)) method, which attempts to convert a string to a number. - [!code-csharp[csrefKeywordsMethodParams#5](../../../../samples/snippets/csharp/language-reference/keywords/out/out-5.cs)] +[!code-csharp-interactive[cs-out-keyword](../../../../samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/OutParameterModifier.cs#4)] Starting with C# 7, you can declare the `out` variable in the argument list of the method call, rather than in a separate variable declaration. This produces more compact, readable code, and also prevents you from inadvertently assigning a value to the variable before the method call. The following example is like the previous example, except that it defines the `number` variable in the call to the [Int32.TryParse](xref:System.Int32.TryParse(System.String,System.Int32@)) method. - [!code-csharp[csrefKeywordsMethodParams#6](../../../../samples/snippets/csharp/language-reference/keywords/out/out-6.cs)] +[!code-csharp-interactive[cs-out-keyword](../../../../samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/OutParameterModifier.cs#5)] In the previous example, the `number` variable is strongly typed as an `int`. You can also declare an implicitly typed local variable, as the following example does. - [!code-csharp[csrefKeywordsMethodParams#7](../../../../samples/snippets/csharp/language-reference/keywords/out/out-7.cs)] +[!code-csharp-interactive[cs-out-keyword](../../../../samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/OutParameterModifier.cs#6)] ## C# Language Specification - [!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] +[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] ## See Also [C# Reference](../../../csharp/language-reference/index.md) diff --git a/docs/csharp/language-reference/keywords/readonly.md b/docs/csharp/language-reference/keywords/readonly.md index 26c980193eef4..0f7703970500c 100644 --- a/docs/csharp/language-reference/keywords/readonly.md +++ b/docs/csharp/language-reference/keywords/readonly.md @@ -18,7 +18,7 @@ ms.author: "wiwagn" # readonly (C# Reference) The `readonly` keyword is a modifier that you can use on fields. When a field declaration includes a `readonly` modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class. -## Example +## Readonly field example In this example, the value of the field `year` cannot be changed in the method `ChangeYear`, even though it is assigned a value in the class constructor: [!code-csharp[csrefKeywordsModifiers#14](../../../csharp/language-reference/keywords/codesnippet/CSharp/readonly_1.cs)] @@ -31,7 +31,7 @@ The `readonly` keyword is a modifier that you can use on fields. When a field de public readonly int y = 5; ``` -- For an instance field, in the instance constructors of the class that contains the field declaration, or for a static field, in the static constructor of the class that contains the field declaration. These are also the only contexts in which it is valid to pass a `readonly` field as an [out](../../../csharp/language-reference/keywords/out.md) or [ref](../../../csharp/language-reference/keywords/ref.md) parameter. +- For an instance field, in the instance constructors of the class that contains the field declaration, or for a static field, in the static constructor of the class that contains the field declaration. These are also the only contexts in which it is valid to pass a `readonly` field as an [out](../../../csharp/language-reference/keywords/out-parameter-modifier.md) or [ref](../../../csharp/language-reference/keywords/ref.md) parameter. > [!NOTE] > The `readonly` keyword is different from the [const](../../../csharp/language-reference/keywords/const.md) keyword. A `const` field can only be initialized at the declaration of the field. A `readonly` field can be initialized either at the declaration or in a constructor. Therefore, `readonly` fields can have different values depending on the constructor used. Also, while a `const` field is a compile-time constant, the `readonly` field can be used for runtime constants as in the following example: @@ -40,7 +40,7 @@ The `readonly` keyword is a modifier that you can use on fields. When a field de public static readonly uint timeStamp = (uint)DateTime.Now.Ticks; ``` -## Example +## Comparing readonly and non-readonly instance fields [!code-csharp[csrefKeywordsModifiers#15](../../../csharp/language-reference/keywords/codesnippet/CSharp/readonly_2.cs)] In the preceding example, if you use a statement like this: diff --git a/docs/csharp/language-reference/keywords/ref.md b/docs/csharp/language-reference/keywords/ref.md index 2079a439fd3f7..f75b0a0bd1494 100644 --- a/docs/csharp/language-reference/keywords/ref.md +++ b/docs/csharp/language-reference/keywords/ref.md @@ -1,6 +1,6 @@ --- title: "ref (C# Reference)" -ms.date: 05/30/2017 +ms.date: 03/06/2018 ms.prod: .net ms.technology: - "devlang-csharp" @@ -11,8 +11,6 @@ f1_keywords: helpviewer_keywords: - "parameters [C#], ref" - "ref keyword [C#]" -ms.assetid: b8a5e59c-907d-4065-b41d-95bf4273c0bd -caps.latest.revision: 32 author: "BillWagner" ms.author: "wiwagn" --- @@ -35,35 +33,41 @@ When used in a method's parameter list, the `ref` keyword indicates that an argu To use a `ref` parameter, both the method definition and the calling method must explicitly use the `ref` keyword, as shown in the following example. -[!code-csharp[csrefKeywordsMethodParams#6](../../../../samples/snippets/csharp/language-reference/keywords/ref/ref-1.cs)] +[!code-csharp-interactive[csrefKeywordsMethodParams#6](../../../../samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/RefParameterModifier.cs#1)] -An argument that is passed to a `ref` parameter must be initialized before it is passed. This differs from [out](out.md) parameters, whose arguments do not have to be explicitly initialized before they are passed. +An argument that is passed to a `ref` or `in` parameter must be initialized before it is passed. This differs from [out](out-parameter-modifier.md) parameters, whose arguments do not have to be explicitly initialized before they are passed. -Members of a class can't have signatures that differ only by `ref` and `out`. A compiler error occurs if the only difference between two members of a type is that one of them has a `ref` parameter and the other has an `out` parameter. The following code, for example, doesn't compile. - - [!code-csharp[csrefKeywordsMethodParams#2](../../../../samples/snippets/csharp/language-reference/keywords/ref/ref-2.cs)] - - However, methods can be overloaded when one method has a `ref` or `out` parameter and the other has a value parameter, as shown in the following example. +Members of a class can't have signatures that differ only by `ref`, `in`, or `out`. A compiler error occurs if the only difference between two members of a type is that one of them has a `ref` parameter and the other has an `out`, or `in` parameter. The following code, for example, doesn't compile. + +```csharp +class CS0663_Example +{ + // Compiler error CS0663: "Cannot define overloaded + // methods that differ only on ref and out". + public void SampleMethod(out int i) { } + public void SampleMethod(ref int i) { } +} +``` +However, methods can be overloaded when one method has a `ref`, `in`, or `out` parameter and the other has a value parameter, as shown in the following example. - [!code-csharp[ref-and-overloads](../../../../samples/snippets/csharp/language-reference/keywords/ref/ref-3.cs)] +[!code-csharp[csrefKeywordsMethodParams#6](../../../../samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/RefParameterModifier.cs#2)] - In other situations that require signature matching, such as hiding or overriding, `ref` and `out` are part of the signature and don't match each other. + In other situations that require signature matching, such as hiding or overriding, `in`, `ref`, and `out` are part of the signature and don't match each other. Properties are not variables. They are methods, and cannot be passed to `ref` parameters. For information about how to pass arrays, see [Passing Arrays Using ref and out](../../../csharp/programming-guide/arrays/passing-arrays-using-ref-and-out.md). - You can't use the `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 can't use the `ref`, `in`, 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. + ## Passing an argument by reference: An example The previous examples pass value types by reference. You can also use the `ref` keyword to pass reference types by reference. Passing a reference type by reference enables the called method to replace the object to which the reference parameter refers in the caller. The storage location of the object is passed to the method as the value of the reference parameter. If you change the value in the storage location of the parameter (to point to a new object), you also change the storage location to which the caller refers. The following example passes an instance of a reference type as a `ref` parameter. - [!code-csharp[ReferencesByRef](../../../../samples/snippets/csharp/language-reference/keywords/ref/ref-4.cs)] +[!code-csharp[csrefKeywordsMethodParams#6](../../../../samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/RefParameterModifier.cs#3)] For more information about how to pass reference types by value and by reference, see [Passing Reference-Type Parameters](../../../csharp/programming-guide/classes-and-structs/passing-reference-type-parameters.md). @@ -106,11 +110,11 @@ Note that the `ref` keyword must be used in both places, or the compiler generat The following example defines a `Book` class that has two fields, `Title` and `Author`. It also defines a `BookCollection` class that includes a private array of `Book` objects. Individual book objects are returned by reference by calling its `GetBookByTitle` method. -[!code-csharp[csrefreturns](../../../../samples/snippets/csharp/language-reference/keywords/ref/ref-5.cs#1)] +[!code-csharp[csrefKeywordsMethodParams#6](../../../../samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/RefParameterModifier.cs#4)] When the caller stores the value returned by the `GetBookByTitle` method as a ref local, changes that the caller makes to the return value are reflected in the `BookCollection` object, as the following example shows. -[!code-csharp[csrefreturns](../../../../samples/snippets/csharp/language-reference/keywords/ref/ref-5.cs#2)] +[!code-csharp[csrefKeywordsMethodParams#6](../../../../samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/RefParameterModifier.cs#5)] ## C# Language Specification [!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] diff --git a/docs/csharp/language-reference/keywords/reference-types.md b/docs/csharp/language-reference/keywords/reference-types.md index a4ca2d98abc6f..1a04b5e5522e6 100644 --- a/docs/csharp/language-reference/keywords/reference-types.md +++ b/docs/csharp/language-reference/keywords/reference-types.md @@ -17,7 +17,7 @@ author: "BillWagner" ms.author: "wiwagn" --- # Reference Types (C# Reference) -There are two kinds of types in C#: reference types and value types. Variables of reference types store references to their data (objects), while variables of value types directly contain their data. With reference types, two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable. With value types, each variable has its own copy of the data, and it is not possible for operations on one variable to affect the other (except in the case of ref and out parameter variables, see [ref](../../../csharp/language-reference/keywords/ref.md) and [out parameter modifier](../../../csharp/language-reference/keywords/out-parameter-modifier.md)). +There are two kinds of types in C#: reference types and value types. Variables of reference types store references to their data (objects), while variables of value types directly contain their data. With reference types, two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable. With value types, each variable has its own copy of the data, and it is not possible for operations on one variable to affect the other (except in the case of in, ref and out parameter variables; see [in](../../../csharp/language-reference/keywords/in-parameter-modifier.md), [ref](../../../csharp/language-reference/keywords/ref.md) and [out](../../../csharp/language-reference/keywords/out-parameter-modifier.md) parameter modifier). The following keywords are used to declare reference types: diff --git a/docs/csharp/language-reference/keywords/toc.md b/docs/csharp/language-reference/keywords/toc.md index cc2605860c911..01a696959db8a 100644 --- a/docs/csharp/language-reference/keywords/toc.md +++ b/docs/csharp/language-reference/keywords/toc.md @@ -86,9 +86,9 @@ ### [lock Statement](lock-statement.md) ## [Method Parameters](method-parameters.md) ### [params](params.md) +### [in (Parameter Modifier)](in-parameter-modifier.md) ### [ref](ref.md) -### [out](out.md) -#### [out (Parameter Modifier)](out-parameter-modifier.md) +### [out (Parameter Modifier)](out-parameter-modifier.md) ## [Namespace Keywords](namespace-keywords.md) ### [namespace](namespace.md) ### [using](using.md) diff --git a/docs/csharp/language-reference/keywords/yield.md b/docs/csharp/language-reference/keywords/yield.md index 6599bc89c416f..363947b183d7c 100644 --- a/docs/csharp/language-reference/keywords/yield.md +++ b/docs/csharp/language-reference/keywords/yield.md @@ -39,7 +39,7 @@ yield break; - The return type must be , , , or . -- The declaration can't have any [ref](../../../csharp/language-reference/keywords/ref.md) or [out](../../../csharp/language-reference/keywords/out.md) parameters. +- The declaration can't have any [in](../../../csharp/language-reference/keywords/in-parameter-modifier.md) [ref](../../../csharp/language-reference/keywords/ref.md) or [out](../../../csharp/language-reference/keywords/out-parameter-modifier.md) parameters. The `yield` type of an iterator that returns or is `object`. If the iterator returns or , there must be an implicit conversion from the type of the expression in the `yield return` statement to the generic type parameter . diff --git a/docs/csharp/methods.md b/docs/csharp/methods.md index 97fc402f8e143..1ea768f89ecc5 100644 --- a/docs/csharp/methods.md +++ b/docs/csharp/methods.md @@ -113,7 +113,7 @@ The following example defines a class (which is a reference type) named `SampleR ### Passing parameters by reference ### -You pass a parameter by reference when you want to change the value of an argument in a method and want to refect that change when control returns to the calling method. To pass a parameter by reference, you use the `ref` or `out` keyword. +You pass a parameter by reference when you want to change the value of an argument in a method and want to refect that change when control returns to the calling method. To pass a parameter by reference, you use the [`ref`](language-reference/keywords/ref.md) or [`out`](language-reference/keywords/out-parameter-modifier.md) keyword. You can also pass a value by reference to avoid copying but still prevent modifications using the [`in`](language-reference/keywords/in-parameter-modifier.md) keyword. The following example is identical to the previous one, except the value is passed by reference to the `ModifyValue` method. When the value of the parameter is modified in the `ModifyValue` method, the change in value is reflected when control returns to the caller. @@ -264,7 +264,7 @@ In the following example, `DelayAsync` is an async method that has a return stat [!code-csharp[csSnippets.Methods#102](../../samples/snippets/csharp/concepts/methods/async1.cs#102)] -An async method can't declare any [ref](language-reference/keywords/ref.md) or [out](language-reference/keywords/out.md) parameters, but it can call methods that have such parameters. +An async method can't declare any [in](language-reference/keywords/in-parameter-modifier.md), [ref](language-reference/keywords/ref.md), or [out](language-reference/keywords/out-parameter-modifier.md) parameters, but it can call methods that have such parameters. For more information about async methods, see [Asynchronous Programming with Async and Await](async.md), [Control Flow in Async Programs](programming-guide/concepts/async/control-flow-in-async-programs.md), and [Async Return Types](programming-guide/concepts/async/async-return-types.md). @@ -300,6 +300,7 @@ For more information, see [Iterators](programming-guide/concepts/iterators.md). [Inheritance](programming-guide/classes-and-structs/inheritance.md) [Abstract and Sealed Classes and Class Members](programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members.md) [params](language-reference/keywords/params.md) -[out](language-reference/keywords/out.md) +[out](language-reference/keywords/out-parameter-modifier.md) [ref](language-reference/keywords/ref.md) +[in](language-reference/keywords/in-parameter-modifier.md) [Passing Parameters](programming-guide/classes-and-structs/passing-parameters.md) diff --git a/docs/csharp/misc/cs0036.md b/docs/csharp/misc/cs0036.md index ee73623e9e9c0..c8c676e519af2 100644 --- a/docs/csharp/misc/cs0036.md +++ b/docs/csharp/misc/cs0036.md @@ -17,7 +17,7 @@ ms.author: "wiwagn" # Compiler Error CS0036 An out parameter cannot have the '[In]' attribute - Currently, the **In** attribute is not permitted on an [out](../../csharp/language-reference/keywords/out.md) parameter. + Currently, the **In** attribute is not permitted on an [out](../../csharp/language-reference/keywords/out-parameter-modifier.md) parameter. The following sample generates CS0036: diff --git a/docs/csharp/misc/cs0177.md b/docs/csharp/misc/cs0177.md index a5e1d645d99be..f255ceaca63c6 100644 --- a/docs/csharp/misc/cs0177.md +++ b/docs/csharp/misc/cs0177.md @@ -17,7 +17,7 @@ ms.author: "wiwagn" # Compiler Error CS0177 The out parameter 'parameter' must be assigned to before control leaves the current method - A parameter marked with the [out](../../csharp/language-reference/keywords/out.md) keyword was not assigned a value in the method body. For more information, see [Passing Parameters](../../csharp/programming-guide/classes-and-structs/passing-parameters.md) + A parameter marked with the [out](../../csharp/language-reference/keywords/out-parameter-modifier.md) keyword was not assigned a value in the method body. For more information, see [Passing Parameters](../../csharp/programming-guide/classes-and-structs/passing-parameters.md) The following sample generates CS0177: diff --git a/docs/csharp/misc/cs0192.md b/docs/csharp/misc/cs0192.md index 48277c60ffd57..6fb9ae12ae6a4 100644 --- a/docs/csharp/misc/cs0192.md +++ b/docs/csharp/misc/cs0192.md @@ -17,7 +17,7 @@ ms.author: "wiwagn" # Compiler Error CS0192 Fields of static readonly field 'name' cannot be passed ref or out (except in a static constructor) - A field (variable) marked with the [readonly](../../csharp/language-reference/keywords/readonly.md) keyword cannot be passed either to a [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out.md) parameter except inside a constructor. For more information, see [Fields](../../csharp/programming-guide/classes-and-structs/fields.md). + A field (variable) marked with the [readonly](../../csharp/language-reference/keywords/readonly.md) keyword cannot be passed either to a [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out-parameter-modifier.md) parameter except inside a constructor. For more information, see [Fields](../../csharp/programming-guide/classes-and-structs/fields.md). CS0192 also results if the `readonly` field is [static](../../csharp/language-reference/keywords/static.md) and the constructor is not marked `static`. diff --git a/docs/csharp/misc/cs0197.md b/docs/csharp/misc/cs0197.md index 0111c332a4cda..d4d12f1d0a7d8 100644 --- a/docs/csharp/misc/cs0197.md +++ b/docs/csharp/misc/cs0197.md @@ -17,7 +17,7 @@ ms.author: "wiwagn" # Compiler Warning (level 1) CS0197 Passing 'argument' as ref or out or taking its address may cause a runtime exception because it is a field of a marshal-by-reference class - Any class that derives, directly or indirectly, from is a marshal-by-reference class. Such a class can be marshaled by reference across process and machine boundaries. Thus, instances of this class could be proxies to remote objects. You cannot pass a field of a proxy object as [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out.md). So, you cannot pass fields of such a class as `ref` or `out`, unless the instance is [this](../../csharp/language-reference/keywords/this.md), which can not be a proxy object. + Any class that derives, directly or indirectly, from is a marshal-by-reference class. Such a class can be marshaled by reference across process and machine boundaries. Thus, instances of this class could be proxies to remote objects. You cannot pass a field of a proxy object as [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out-parameter-modifier.md). So, you cannot pass fields of such a class as `ref` or `out`, unless the instance is [this](../../csharp/language-reference/keywords/this.md), which can not be a proxy object. ## Example The following sample generates CS0197. diff --git a/docs/csharp/misc/cs0199.md b/docs/csharp/misc/cs0199.md index 17f00cc2f1078..88a6adc8bd3b2 100644 --- a/docs/csharp/misc/cs0199.md +++ b/docs/csharp/misc/cs0199.md @@ -17,7 +17,7 @@ ms.author: "wiwagn" # Compiler Error CS0199 Fields of static readonly field 'name' cannot be passed ref or out (except in a static constructor) - A [readonly](../../csharp/language-reference/keywords/readonly.md) variable must have the same [static](../../csharp/language-reference/keywords/static.md) usage as the constructor in which you want to pass it as a [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out.md) parameter. For more information, see [Passing Parameters](../../csharp/programming-guide/classes-and-structs/passing-parameters.md). + A [readonly](../../csharp/language-reference/keywords/readonly.md) variable must have the same [static](../../csharp/language-reference/keywords/static.md) usage as the constructor in which you want to pass it as a [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out-parameter-modifier.md) parameter. For more information, see [Passing Parameters](../../csharp/programming-guide/classes-and-structs/passing-parameters.md). ## Example The following sample generates CS0199: diff --git a/docs/csharp/misc/cs0206.md b/docs/csharp/misc/cs0206.md index 88b50461de153..ef77d1c488c99 100644 --- a/docs/csharp/misc/cs0206.md +++ b/docs/csharp/misc/cs0206.md @@ -17,7 +17,7 @@ ms.author: "wiwagn" # Compiler Error CS0206 A property or indexer may not be passed as an out or ref parameter - A [property](../../csharp/programming-guide/classes-and-structs/properties.md) is not available to be passed as a [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out.md) parameter. For more information, see [Passing Parameters](../../csharp/programming-guide/classes-and-structs/passing-parameters.md). + A [property](../../csharp/programming-guide/classes-and-structs/properties.md) is not available to be passed as a [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out-parameter-modifier.md) parameter. For more information, see [Passing Parameters](../../csharp/programming-guide/classes-and-structs/passing-parameters.md). ## Example The following sample generates CS0206: diff --git a/docs/csharp/misc/cs0631.md b/docs/csharp/misc/cs0631.md index b9043662c8f9d..1ebfacf276598 100644 --- a/docs/csharp/misc/cs0631.md +++ b/docs/csharp/misc/cs0631.md @@ -17,7 +17,7 @@ ms.author: "wiwagn" # Compiler Error CS0631 ref and out are not valid in this context - The declaration for an [indexer](../../csharp/programming-guide/indexers/index.md) cannot include the use of [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out.md) parameters. + The declaration for an [indexer](../../csharp/programming-guide/indexers/index.md) cannot include the use of [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out-parameter-modifier.md) parameters. ## Example The following sample generates CS0631: diff --git a/docs/csharp/misc/cs0663.md b/docs/csharp/misc/cs0663.md index 66e4c6c71dd87..7de27a8a3884d 100644 --- a/docs/csharp/misc/cs0663.md +++ b/docs/csharp/misc/cs0663.md @@ -17,7 +17,7 @@ ms.author: "wiwagn" # Compiler Error CS0663 Cannot define overloaded methods that differ only on ref and out. - Methods that differ only on their use of [ref](../../csharp/language-reference/keywords/ref.md) and [out](../../csharp/language-reference/keywords/out.md) on a parameter are not allowed. + Methods that differ only on their use of [in](../../csharp/language-reference/keywords/in-parameter-modifier.md), [ref](../../csharp/language-reference/keywords/ref.md) and [out](../../csharp/language-reference/keywords/out-parameter-modifier.md) on a parameter are not allowed. The following sample generates CS0663: diff --git a/docs/csharp/misc/cs0752.md b/docs/csharp/misc/cs0752.md index 4f8252f2a24af..984a2213db818 100644 --- a/docs/csharp/misc/cs0752.md +++ b/docs/csharp/misc/cs0752.md @@ -17,7 +17,7 @@ ms.author: "wiwagn" # Compiler Error CS0752 A partial method cannot have out parameters - A partial method cannot have an [out](../../csharp/language-reference/keywords/out.md) parameter. Out parameters are not allowed because if the partial method is removed by the compiler then there is no guarantee that the out parameter is ever assigned. + A partial method cannot have an [out](../../csharp/language-reference/keywords/out-parameter-modifier.md) parameter. Out parameters are not allowed because if the partial method is removed by the compiler then there is no guarantee that the out parameter is ever assigned. ## To correct this error diff --git a/docs/csharp/misc/cs1107.md b/docs/csharp/misc/cs1107.md index 0aa79ba0c2b60..7e87091e1523e 100644 --- a/docs/csharp/misc/cs1107.md +++ b/docs/csharp/misc/cs1107.md @@ -17,20 +17,26 @@ ms.author: "wiwagn" # Compiler Error CS1107 A parameter can only have one 'modifier name' modifier. - It is an error for parameter modifiers such as `this`, `ref`, and `out` to appear more than one time in a parameter definition. + It is an error for parameter modifiers such as `this`, `ref`, `in`, and `out` to appear more than one time in a parameter definition. ## Example The following example generates CS1107: ``` // cs1107.cs -public static class Test -{ +public static class Test +{ // Extension methods. - public static void TestMethod(this this t) {} // CS1107 - + public static void TestMethod(this this t) { } // CS1107 + +} + +public class TestTwo +{ // Regular Instance Method - public void TestMethod(ref ref int i) {} // CS1107 - -} + public void TestMethod(ref ref int i) { } // CS1107 + + // Regular Instance Method + public void TestMethod(in in double d) { } // CS1107 +} ``` diff --git a/docs/csharp/misc/cs1108.md b/docs/csharp/misc/cs1108.md index ccdf13d514174..5dce1012bca93 100644 --- a/docs/csharp/misc/cs1108.md +++ b/docs/csharp/misc/cs1108.md @@ -17,7 +17,7 @@ ms.author: "wiwagn" # Compiler Error CS1108 A parameter cannot have all the specified modifiers; there are too many modifiers on the parameter. - Certain combinations of parameter modifiers, such as `ref` and `out`, are not allowed because they have mutually exclusive meanings for the compiler. + Certain combinations of parameter modifiers, such as `in`, `ref`, and `out`, are not allowed because they have mutually exclusive meanings for the compiler. ## Example The following example generates CS1108: diff --git a/docs/csharp/misc/cs1601.md b/docs/csharp/misc/cs1601.md index 8c562b92f143a..87714f530ca85 100644 --- a/docs/csharp/misc/cs1601.md +++ b/docs/csharp/misc/cs1601.md @@ -17,22 +17,27 @@ ms.author: "wiwagn" # Compiler Error CS1601 Method or delegate parameter cannot be of type 'type' - Some types in the .NET Framework class library, for example, , and cannot be used as [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out.md) parameters because they could potentially be used to perform unsafe operations. + Some types in the .NET Framework class library, for example, , and cannot be used as [in](../../csharp/language-reference/keywords/in-parameter-modifier.md), [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out-parameter-modifier.md) parameters because they could potentially be used to perform unsafe operations. The following sample generates CS1601: ``` // CS1601.cs -using System; - -class MyClass -{ - public void Test1 (ref TypedReference t) // CS1601 - { - } - - public void Test2 (out ArgIterator t) // CS1601 - { - } -} +using System; + +class MyClass +{ + public void Test1(in TypedReference t) // CS1601 + { + } + + public void Test2(ref TypedReference t) // CS1601 + { + } + + public void Test3(out ArgIterator t) // CS1601 + { + t = default; + } +} ``` diff --git a/docs/csharp/misc/cs1605.md b/docs/csharp/misc/cs1605.md index be347f57080d7..9560475509db6 100644 --- a/docs/csharp/misc/cs1605.md +++ b/docs/csharp/misc/cs1605.md @@ -17,4 +17,4 @@ ms.author: "wiwagn" # Compiler Error CS1605 Cannot pass 'var' as a ref or out argument because it is read-only - A variable passed as a [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out.md) parameter is expected to be modified in the called method. Therefore, it is not possible to pass a read-only parameter as `ref` or `out`. + A variable passed as a [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out-parameter-modifier.md) parameter is expected to be modified in the called method. Therefore, it is not possible to pass a read-only parameter as `ref` or `out`. diff --git a/docs/csharp/misc/cs1611.md b/docs/csharp/misc/cs1611.md index d93eb5c4cc0d2..480862646d800 100644 --- a/docs/csharp/misc/cs1611.md +++ b/docs/csharp/misc/cs1611.md @@ -15,9 +15,9 @@ author: "BillWagner" ms.author: "wiwagn" --- # Compiler Error CS1611 -The params parameter cannot be declared as ref or out +The params parameter cannot be declared as in ref or out - The keywords [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out.md) cannot be used with the [params](../../csharp/language-reference/keywords/params.md) keyword. + The keywords [in](../../csharp/language-reference/keywords/in-parameter-modifier.md), [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out-parameter-modifier.md) cannot be used with the [params](../../csharp/language-reference/keywords/params.md) keyword. The following sample generates CS1611: diff --git a/docs/csharp/misc/cs1620.md b/docs/csharp/misc/cs1620.md index 2fcef6f9fb311..61cb96c8a72e8 100644 --- a/docs/csharp/misc/cs1620.md +++ b/docs/csharp/misc/cs1620.md @@ -17,7 +17,7 @@ ms.author: "wiwagn" # Compiler Error CS1620 Argument 'number' must be passed with the 'keyword' keyword - This error occurs if you are passing an argument to a function that takes a [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out.md) parameter and you don't include the `ref` or `out` keyword at the point of call, or you include the wrong keyword. The error text indicates the appropriate keyword to use and which argument caused the failure. + This error occurs if you are passing an argument to a function that takes a [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out-parameter-modifier.md) parameter and you don't include the `ref` or `out` keyword at the point of call, or you include the wrong keyword. The error text indicates the appropriate keyword to use and which argument caused the failure. The following sample generates CS1620: diff --git a/docs/csharp/misc/cs1623.md b/docs/csharp/misc/cs1623.md index af9146f3d04dc..ac3a4a3407143 100644 --- a/docs/csharp/misc/cs1623.md +++ b/docs/csharp/misc/cs1623.md @@ -15,35 +15,40 @@ author: "BillWagner" ms.author: "wiwagn" --- # Compiler Error CS1623 -Iterators cannot have ref or out parameters +Iterators cannot have in ref or out parameters - This error occurs if an iterator method takes a `ref` or `out` parameter. To avoid this error, remove the `ref` or `out` keyword from the method signature. + This error occurs if an iterator method takes an `in`, `ref`, or `out` parameter. To avoid this error, remove the `in`, `ref`, or `out` keyword from the method signature. ## Example The following sample generates CS1623: ``` // CS1623.cs -using System.Collections; - -class C : IEnumerable -{ - public IEnumerator GetEnumerator() - { - yield return 0; - } - +using System.Collections; + +class C : IEnumerable +{ + public IEnumerator GetEnumerator() + { + yield return 0; + } + + // To resolve the error, remove in + public IEnumerator GetEnumerator(in short i) // CS1623 + { + yield return i; + } // To resolve the error, remove ref public IEnumerator GetEnumerator(ref int i) // CS1623 - { - yield return i; - } - + { + yield return i; + } + // To resolve the error, remove out public IEnumerator GetEnumerator(out float f) // CS1623 - { - f = 0.0F; - yield return f; - } -} + { + f = 0.0F; + yield return f; + } +} ``` diff --git a/docs/csharp/misc/cs1628.md b/docs/csharp/misc/cs1628.md index 46c1fdb05e737..f0e3597930054 100644 --- a/docs/csharp/misc/cs1628.md +++ b/docs/csharp/misc/cs1628.md @@ -15,9 +15,9 @@ author: "BillWagner" ms.author: "wiwagn" --- # Compiler Error CS1628 -Cannot use ref or out parameter 'parameter' inside an anonymous method, lambda expression, or query expression +Cannot use in ref or out parameter 'parameter' inside an anonymous method, lambda expression, or query expression - This error occurs if you use a ref or out parameter inside an anonymous method block. To avoid this error, use a local variable or some other construct. + This error occurs if you use an `in`, `ref`, or `out` parameter inside an anonymous method block. To avoid this error, use a local variable or some other construct. The following sample generates CS1628: diff --git a/docs/csharp/misc/cs1657.md b/docs/csharp/misc/cs1657.md index 8c53e0837d2a6..61364807623f5 100644 --- a/docs/csharp/misc/cs1657.md +++ b/docs/csharp/misc/cs1657.md @@ -17,7 +17,7 @@ ms.author: "wiwagn" # Compiler Error CS1657 Cannot pass 'parameter' as a ref or out argument because 'reason'' - This error occurs when a variable is passed as a [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out.md) argument in a context in which that variable is readonly. Readonly contexts include [foreach](../../csharp/language-reference/keywords/foreach-in.md) iteration variables, [using](../../csharp/language-reference/keywords/using-statement.md) variables, and `fixed` variables. To resolve this error, do not call functions that take the `foreach`, `using` or `fixed` variable as a `ref` or `out` parameter in `using` blocks, `foreach` statements, and `fixed` statements. + This error occurs when a variable is passed as a [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out-parameter-modifier.md) argument in a context in which that variable is readonly. Readonly contexts include [foreach](../../csharp/language-reference/keywords/foreach-in.md) iteration variables, [using](../../csharp/language-reference/keywords/using-statement.md) variables, and `fixed` variables. To resolve this error, do not call functions that take the `foreach`, `using` or `fixed` variable as a `ref` or `out` parameter in `using` blocks, `foreach` statements, and `fixed` statements. ## Example The following sample generates CS1657: diff --git a/docs/csharp/misc/cs1951.md b/docs/csharp/misc/cs1951.md index 9355d700c7b84..b644c66d074a7 100644 --- a/docs/csharp/misc/cs1951.md +++ b/docs/csharp/misc/cs1951.md @@ -15,13 +15,13 @@ author: "BillWagner" ms.author: "wiwagn" --- # Compiler Error CS1951 -An expression tree lambda may not contain an out or ref parameter. +An expression tree lambda may not contain an in, out, or ref parameter. An expression tree just represents expressions as data structures. There is no way to represent specific memory locations as is required when you pass a parameter by reference. ## To correct this error -1. The only option is to remove the `ref` modifier in the delegate definition and pass in the parameter by value. +1. The only option is to remove the modifier in the delegate definition and pass the parameter by value. ## Example The following example generates CS1951: diff --git a/docs/csharp/misc/cs3006.md b/docs/csharp/misc/cs3006.md index 7e51720f0e7af..59aea9f496982 100644 --- a/docs/csharp/misc/cs3006.md +++ b/docs/csharp/misc/cs3006.md @@ -17,7 +17,7 @@ ms.author: "wiwagn" # Compiler Warning (level 1) CS3006 Overloaded method 'method' differing only in ref or out, or in array rank, is not CLS-compliant - A method does not cannot be overloaded based on the [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out.md) parameter and still comply with the Common Language Specification (CLS). For more information on CLS Compliance, see [Writing CLS-Compliant Code](http://msdn.microsoft.com/library/4c705105-69a2-4e5e-b24e-0633bc32c7f3) and [Language Independence and Language-Independent Components](../../../docs/standard/language-independence-and-language-independent-components.md). + A method does not cannot be overloaded based on the [ref](../../csharp/language-reference/keywords/ref.md) or [out](../../csharp/language-reference/keywords/out-parameter-modifier.md) parameter and still comply with the Common Language Specification (CLS). For more information on CLS Compliance, see [Writing CLS-Compliant Code](http://msdn.microsoft.com/library/4c705105-69a2-4e5e-b24e-0633bc32c7f3) and [Language Independence and Language-Independent Components](../../../docs/standard/language-independence-and-language-independent-components.md). ## Example The following example generates CS3006. To resolve this warning, comment out the assembly-level attribute or remove one of the method definitions. diff --git a/docs/csharp/programming-guide/arrays/passing-arrays-using-ref-and-out.md b/docs/csharp/programming-guide/arrays/passing-arrays-using-ref-and-out.md index afd4620c52490..d27b1e5c14c62 100644 --- a/docs/csharp/programming-guide/arrays/passing-arrays-using-ref-and-out.md +++ b/docs/csharp/programming-guide/arrays/passing-arrays-using-ref-and-out.md @@ -13,7 +13,7 @@ author: "BillWagner" ms.author: "wiwagn" --- # Passing Arrays Using ref and out (C# Programming Guide) -Like all [out](../../../csharp/language-reference/keywords/out.md) parameters, an `out` parameter of an array type must be assigned before it is used; that is, it must be assigned by the callee. For example: +Like all [out](../../../csharp/language-reference/keywords/out-parameter-modifier.md) parameters, an `out` parameter of an array type must be assigned before it is used; that is, it must be assigned by the callee. For example: [!code-csharp[csProgGuideArrays#39](../../../csharp/programming-guide/arrays/codesnippet/CSharp/passing-arrays-using-ref-and-out_1.cs)] diff --git a/docs/csharp/programming-guide/classes-and-structs/methods.md b/docs/csharp/programming-guide/classes-and-structs/methods.md index 25b993498ac28..529f36a4ee41b 100644 --- a/docs/csharp/programming-guide/classes-and-structs/methods.md +++ b/docs/csharp/programming-guide/classes-and-structs/methods.md @@ -126,7 +126,7 @@ static void Main(string[] args) [!code-csharp[csAsyncMethod#2](../../../csharp/programming-guide/classes-and-structs/codesnippet/CSharp/methods_9.cs)] - An async method can't declare any [ref](../../../csharp/language-reference/keywords/ref.md) or [out](../../../csharp/language-reference/keywords/out.md) parameters, but it can call methods that have such parameters. + An async method can't declare any [ref](../../../csharp/language-reference/keywords/ref.md) or [out](../../../csharp/language-reference/keywords/out-parameter-modifier.md) parameters, but it can call methods that have such parameters. For more information about async methods, see [Asynchronous Programming with async and await](../../../csharp/programming-guide/concepts/async/index.md), [Control Flow in Async Programs](../../../csharp/programming-guide/concepts/async/control-flow-in-async-programs.md), and [Async Return Types](../../../csharp/programming-guide/concepts/async/async-return-types.md). diff --git a/docs/csharp/programming-guide/classes-and-structs/partial-classes-and-methods.md b/docs/csharp/programming-guide/classes-and-structs/partial-classes-and-methods.md index 4390f27f24f68..4b8e70a5dc2a1 100644 --- a/docs/csharp/programming-guide/classes-and-structs/partial-classes-and-methods.md +++ b/docs/csharp/programming-guide/classes-and-structs/partial-classes-and-methods.md @@ -146,7 +146,7 @@ partial void onNameChanged() - Partial method declarations must begin with the contextual keyword [partial](../../../csharp/language-reference/keywords/partial-type.md) and the method must return [void](../../../csharp/language-reference/keywords/void.md). -- Partial methods can have [ref](../../../csharp/language-reference/keywords/ref.md) but not [out](../../../csharp/language-reference/keywords/out.md) parameters. +- Partial methods can have [in](../../../csharp/language-reference/keywords/in-parameter-modifier.md) or [ref](../../../csharp/language-reference/keywords/ref.md) but not [out](../../../csharp/language-reference/keywords/out-parameter-modifier.md) parameters. - Partial methods are implicitly [private](../../../csharp/language-reference/keywords/private.md), and therefore they cannot be [virtual](../../../csharp/language-reference/keywords/virtual.md). diff --git a/docs/csharp/programming-guide/classes-and-structs/passing-parameters.md b/docs/csharp/programming-guide/classes-and-structs/passing-parameters.md index ce42b7797c6b2..5585f20f08c27 100644 --- a/docs/csharp/programming-guide/classes-and-structs/passing-parameters.md +++ b/docs/csharp/programming-guide/classes-and-structs/passing-parameters.md @@ -17,7 +17,7 @@ author: "BillWagner" ms.author: "wiwagn" --- # Passing Parameters (C# Programming Guide) -In C#, arguments can be passed to parameters either by value or by reference. Passing by reference enables function members, methods, properties, indexers, operators, and constructors to change the value of the parameters and have that change persist in the calling environment. To pass a parameter by reference, use the `ref` or `out` keyword. For simplicity, only the `ref` keyword is used in the examples in this topic. For more information about the difference between `ref` and `out`, see [ref](../../../csharp/language-reference/keywords/ref.md), [out](../../../csharp/language-reference/keywords/out.md), and [Passing Arrays Using ref and out](../../../csharp/programming-guide/arrays/passing-arrays-using-ref-and-out.md). +In C#, arguments can be passed to parameters either by value or by reference. Passing by reference enables function members, methods, properties, indexers, operators, and constructors to change the value of the parameters and have that change persist in the calling environment. To pass a parameter by reference with the intent of changing the value, use the `ref`, or `out` keyword. To pass by reference with the intent of avoiding copying but not changing the value, use the `in` modifier. For simplicity, only the `ref` keyword is used in the examples in this topic. For more information about the difference between `in`, `ref`, and `out`, see [in](../../../csharp/language-reference/keywords/in-parameter-modifier.md), [ref](../../../csharp/language-reference/keywords/ref.md), [out](../../../csharp/language-reference/keywords/out-parameter-modifier.md), and [Passing Arrays Using ref and out](../../../csharp/programming-guide/arrays/passing-arrays-using-ref-and-out.md). The following example illustrates the difference between value and reference parameters. diff --git a/docs/csharp/programming-guide/classes-and-structs/passing-reference-type-parameters.md b/docs/csharp/programming-guide/classes-and-structs/passing-reference-type-parameters.md index 8cb0003c754b8..332eefa00aedf 100644 --- a/docs/csharp/programming-guide/classes-and-structs/passing-reference-type-parameters.md +++ b/docs/csharp/programming-guide/classes-and-structs/passing-reference-type-parameters.md @@ -14,7 +14,7 @@ author: "BillWagner" ms.author: "wiwagn" --- # Passing Reference-Type Parameters (C# Programming Guide) -A variable of a [reference type](../../../csharp/language-reference/keywords/reference-types.md) does not contain its data directly; it contains a reference to its data. When you pass a reference-type parameter by value, it is possible to change the data pointed to by the reference, such as the value of a class member. However, you cannot change the value of the reference itself; that is, you cannot use the same reference to allocate memory for a new class and have it persist outside the block. To do that, pass the parameter using the [ref](../../../csharp/language-reference/keywords/ref.md) or [out](../../../csharp/language-reference/keywords/out.md) keyword. For simplicity, the following examples use `ref`. +A variable of a [reference type](../../../csharp/language-reference/keywords/reference-types.md) does not contain its data directly; it contains a reference to its data. When you pass a reference-type parameter by value, it is possible to change the data belonging to the referenced object, such as the value of a class member. However, you cannot change the value of the reference itself; for example, you cannot use the same reference to allocate memory for a new class and have it persist outside the method. To do that, pass the parameter using the [ref](../../../csharp/language-reference/keywords/ref.md) or [out](../../../csharp/language-reference/keywords/out-parameter-modifier.md) keyword. For simplicity, the following examples use `ref`. ## Passing Reference Types by Value The following example demonstrates passing a reference-type parameter, `arr`, by value, to a method, `Change`. Because the parameter is a reference to `arr`, it is possible to change the values of the array elements. However, the attempt to reassign the parameter to a different memory location only works inside the method and does not affect the original variable, `arr`. @@ -44,4 +44,6 @@ A variable of a [reference type](../../../csharp/language-reference/keywords/ref [Passing Parameters](../../../csharp/programming-guide/classes-and-structs/passing-parameters.md) [Passing Arrays Using ref and out](../../../csharp/programming-guide/arrays/passing-arrays-using-ref-and-out.md) [ref](../../../csharp/language-reference/keywords/ref.md) + [in](../../../csharp/language-reference/keywords/in-parameter-modifier.md) + [out](../../../csharp/language-reference/keywords/out.md) [Reference Types](../../../csharp/language-reference/keywords/reference-types.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/passing-value-type-parameters.md b/docs/csharp/programming-guide/classes-and-structs/passing-value-type-parameters.md index 029a66f40e094..c73baebd9477e 100644 --- a/docs/csharp/programming-guide/classes-and-structs/passing-value-type-parameters.md +++ b/docs/csharp/programming-guide/classes-and-structs/passing-value-type-parameters.md @@ -14,7 +14,7 @@ author: "BillWagner" ms.author: "wiwagn" --- # Passing Value-Type Parameters (C# Programming Guide) -A [value-type](../../../csharp/language-reference/keywords/value-types.md) variable contains its data directly as opposed to a [reference-type](../../../csharp/language-reference/keywords/reference-types.md) variable, which contains a reference to its data. Passing a value-type variable to a method by value means passing a copy of the variable to the method. Any changes to the parameter that take place inside the method have no affect on the original data stored in the argument variable. If you want the called method to change the value of the parameter, you must pass it by reference, using the [ref](../../../csharp/language-reference/keywords/ref.md) or [out](../../../csharp/language-reference/keywords/out.md) keyword. For simplicity, the following examples use `ref`. +A [value-type](../../../csharp/language-reference/keywords/value-types.md) variable contains its data directly as opposed to a [reference-type](../../../csharp/language-reference/keywords/reference-types.md) variable, which contains a reference to its data. Passing a value-type variable to a method by value means passing a copy of the variable to the method. Any changes to the parameter that take place inside the method have no affect on the original data stored in the argument variable. If you want the called method to change the value of the parameter, you must pass it by reference, using the [ref](../../../csharp/language-reference/keywords/ref.md) or [out](../../../csharp/language-reference/keywords/out-parameter-modifier.md) keyword. You may also use the [in](../../../csharp/language-reference/keywords/out-parameter-modifier.md) keyword to pass a value parameter by reference to avoid the copy while guaranteeing that the value will not be changed. For simplicity, the following examples use `ref`. ## Passing Value Types by Value The following example demonstrates passing value-type parameters by value. The variable `n` is passed by value to the method `SquareIt`. Any changes that take place inside the method have no affect on the original value of the variable. diff --git a/docs/csharp/programming-guide/classes-and-structs/using-properties.md b/docs/csharp/programming-guide/classes-and-structs/using-properties.md index f9c4730e996f2..c6a5f5fe6379d 100644 --- a/docs/csharp/programming-guide/classes-and-structs/using-properties.md +++ b/docs/csharp/programming-guide/classes-and-structs/using-properties.md @@ -17,7 +17,7 @@ ms.author: "wiwagn" # Using Properties (C# Programming Guide) Properties combine aspects of both fields and methods. To the user of an object, a property appears to be a field, accessing the property requires the same syntax. To the implementer of a class, a property is one or two code blocks, representing a [get](../../../csharp/language-reference/keywords/get.md) accessor and/or a [set](../../../csharp/language-reference/keywords/set.md) accessor. The code block for the `get` accessor is executed when the property is read; the code block for the `set` accessor is executed when the property is assigned a new value. A property without a `set` accessor is considered read-only. A property without a `get` accessor is considered write-only. A property that has both accessors is read-write. - Unlike fields, properties are not classified as variables. Therefore, you cannot pass a property as a [ref](../../../csharp/language-reference/keywords/ref.md) or [out](../../../csharp/language-reference/keywords/out.md) parameter. + Unlike fields, properties are not classified as variables. Therefore, you cannot pass a property as a [ref](../../../csharp/language-reference/keywords/ref.md) or [out](../../../csharp/language-reference/keywords/out-parameter-modifier.md) parameter. Properties have many uses: they can validate data before allowing a change; they can transparently expose data on a class where that data is actually retrieved from some other source, such as a database; they can take an action when data is changed, such as raising an event, or changing the value of other fields. diff --git a/docs/csharp/programming-guide/concepts/async/index.md b/docs/csharp/programming-guide/concepts/async/index.md index d54de9de01254..1ef1ae31e3091 100644 --- a/docs/csharp/programming-guide/concepts/async/index.md +++ b/docs/csharp/programming-guide/concepts/async/index.md @@ -214,7 +214,7 @@ An async method can also have a `void` return type. This return type is used pri An async method that has a `void` return type can’t be awaited, and the caller of a void-returning method can't catch any exceptions that the method throws. -An async method can't declare [ref](../../../../csharp/language-reference/keywords/ref.md) or [out](../../../../csharp/language-reference/keywords/out.md) parameters, but the method can call methods that have such parameters. Similarly, an async method can't return a value by reference, although it can call methods with ref return values. +An async method can't declare [in](../../../../csharp/language-reference/keywords/in-parameter-modifier.md), [ref](../../../../csharp/language-reference/keywords/ref.md) or [out](../../../../csharp/language-reference/keywords/out-parameter-modifier.md) parameters, but the method can call methods that have such parameters. Similarly, an async method can't return a value by reference, although it can call methods with ref return values. For more information and examples, see [Async Return Types (C#)](../../../../csharp/programming-guide/concepts/async/async-return-types.md). For more information about how to catch exceptions in async methods, see [try-catch](../../../../csharp/language-reference/keywords/try-catch.md). diff --git a/docs/csharp/programming-guide/concepts/covariance-contravariance/creating-variant-generic-interfaces.md b/docs/csharp/programming-guide/concepts/covariance-contravariance/creating-variant-generic-interfaces.md index 3d99cd8502a43..6075369a8d32c 100644 --- a/docs/csharp/programming-guide/concepts/covariance-contravariance/creating-variant-generic-interfaces.md +++ b/docs/csharp/programming-guide/concepts/covariance-contravariance/creating-variant-generic-interfaces.md @@ -23,7 +23,7 @@ You can declare generic type parameters in interfaces as covariant or contravari You can declare variant generic interfaces by using the `in` and `out` keywords for generic type parameters. > [!IMPORTANT] -> `ref` and `out` parameters in C# cannot be variant. Value types also do not support variance. +> `ref`, `in`, and `out` parameters in C# cannot be variant. Value types also do not support variance. You can declare a generic type parameter covariant by using the `out` keyword. The covariant type must satisfy the following conditions: diff --git a/docs/csharp/programming-guide/concepts/covariance-contravariance/variance-in-delegates.md b/docs/csharp/programming-guide/concepts/covariance-contravariance/variance-in-delegates.md index 366ae3547bca4..04b1f12c20a9b 100644 --- a/docs/csharp/programming-guide/concepts/covariance-contravariance/variance-in-delegates.md +++ b/docs/csharp/programming-guide/concepts/covariance-contravariance/variance-in-delegates.md @@ -144,7 +144,7 @@ public delegate void DContravariant(A a); ``` > [!IMPORTANT] -> `ref` and `out` parameters in C# can't be marked as variant. +> `ref`, `in`, and `out` parameters in C# can't be marked as variant. It is also possible to support both variance and covariance in the same delegate, but for different type parameters. This is shown in the following example. diff --git a/docs/csharp/programming-guide/concepts/iterators.md b/docs/csharp/programming-guide/concepts/iterators.md index 8893301ddd1fb..08b046bd92e8b 100644 --- a/docs/csharp/programming-guide/concepts/iterators.md +++ b/docs/csharp/programming-guide/concepts/iterators.md @@ -340,7 +340,7 @@ public class Stack : IEnumerable An implicit conversion must exist from the expression type in the `yield return` statement to the return type of the iterator. - In C#, an iterator method cannot have any `ref` or `out` parameters. + In C#, an iterator method cannot have any `in`, `ref`, or `out` parameters. In C#, "yield" is not a reserved word and has special meaning only when it is used before a `return` or `break` keyword. diff --git a/docs/csharp/programming-guide/delegates/delegates-with-named-vs-anonymous-methods.md b/docs/csharp/programming-guide/delegates/delegates-with-named-vs-anonymous-methods.md index 90d4dff638901..54096ef201ae0 100644 --- a/docs/csharp/programming-guide/delegates/delegates-with-named-vs-anonymous-methods.md +++ b/docs/csharp/programming-guide/delegates/delegates-with-named-vs-anonymous-methods.md @@ -25,7 +25,7 @@ A [delegate](../../../csharp/language-reference/keywords/delegate.md) can be ass A delegate instance may encapsulate either static or instance method. - Although the delegate can use an [out](../../../csharp/language-reference/keywords/out.md) parameter, we do not recommend its use with multicast event delegates because you cannot know which delegate will be called. + Although the delegate can use an [out](../../../csharp/language-reference/keywords/out-parameter-modifier.md) parameter, we do not recommend its use with multicast event delegates because you cannot know which delegate will be called. ## Example 1 The following is a simple example of declaring and using a delegate. Notice that both the delegate, `Del`, and the associated method, `MultiplyNumbers`, have the same signature diff --git a/docs/csharp/programming-guide/indexers/using-indexers.md b/docs/csharp/programming-guide/indexers/using-indexers.md index ab71371933c2a..14524646d07a6 100644 --- a/docs/csharp/programming-guide/indexers/using-indexers.md +++ b/docs/csharp/programming-guide/indexers/using-indexers.md @@ -31,7 +31,7 @@ public int this[int index] // Indexer declaration The signature of an indexer consists of the number and types of its formal parameters. It does not include the indexer type or the names of the formal parameters. If you declare more than one indexer in the same class, they must have different signatures. - An indexer value is not classified as a variable; therefore, you cannot pass an indexer value as a [ref](../../../csharp/language-reference/keywords/ref.md) or [out](../../../csharp/language-reference/keywords/out.md) parameter. + An indexer value is not classified as a variable; therefore, you cannot pass an indexer value as a [ref](../../../csharp/language-reference/keywords/ref.md) or [out](../../../csharp/language-reference/keywords/out-parameter-modifier.md) parameter. To provide the indexer with a name that other languages can use, use a `name` attribute in the declaration. For example: diff --git a/docs/csharp/programming-guide/statements-expressions-operators/anonymous-methods.md b/docs/csharp/programming-guide/statements-expressions-operators/anonymous-methods.md index 8835dceea26ed..9f794c9adf836 100644 --- a/docs/csharp/programming-guide/statements-expressions-operators/anonymous-methods.md +++ b/docs/csharp/programming-guide/statements-expressions-operators/anonymous-methods.md @@ -40,7 +40,7 @@ In versions of C# before 2.0, the only way to declare a [delegate](../../../csha A reference to the outer variable `n` is said to be *captured* when the delegate is created. Unlike local variables, the lifetime of a captured variable extends until the delegates that reference the anonymous methods are eligible for garbage collection. - An anonymous method cannot access the [ref](../../../csharp/language-reference/keywords/ref.md) or [out](../../../csharp/language-reference/keywords/out.md) parameters of an outer scope. + An anonymous method cannot access the [in](../../../csharp/language-reference/keywords/in.md), [ref](../../../csharp/language-reference/keywords/ref.md) or [out](../../../csharp/language-reference/keywords/out-parameter-modifier.md) parameters of an outer scope. No unsafe code can be accessed within the *anonymous-method-block*. diff --git a/docs/csharp/programming-guide/statements-expressions-operators/lambda-expressions.md b/docs/csharp/programming-guide/statements-expressions-operators/lambda-expressions.md index cd235cd5168a6..0ea37d371a0eb 100644 --- a/docs/csharp/programming-guide/statements-expressions-operators/lambda-expressions.md +++ b/docs/csharp/programming-guide/statements-expressions-operators/lambda-expressions.md @@ -260,7 +260,7 @@ class Test - Variables introduced within a lambda expression are not visible in the outer method. -- A lambda expression cannot directly capture a `ref` or `out` parameter from an enclosing method. +- A lambda expression cannot directly capture an `in`, `ref`, or `out` parameter from an enclosing method. - A return statement in a lambda expression does not cause the enclosing method to return. diff --git a/docs/csharp/programming-guide/unsafe-code-pointers/pointer-types.md b/docs/csharp/programming-guide/unsafe-code-pointers/pointer-types.md index 2bce5d77ff44b..137eaebed4a97 100644 --- a/docs/csharp/programming-guide/unsafe-code-pointers/pointer-types.md +++ b/docs/csharp/programming-guide/unsafe-code-pointers/pointer-types.md @@ -109,7 +109,7 @@ Console.ReadLine(); A pointer can be `null`. Applying the indirection operator to a null pointer causes an implementation-defined behavior. - Be aware that passing pointers between methods can cause undefined behavior. Examples are returning a pointer to a local variable through an Out or Ref parameter or as the function result. If the pointer was set in a fixed block, the variable to which it points may no longer be fixed. + Be aware that passing pointers between methods can cause undefined behavior. Consider a method that returns a pointer to a local variable through an `in`, `out` or `ref` parameter or as the function result. If the pointer was set in a fixed block, the variable to which it points may no longer be fixed. The following table lists the operators and statements that can operate on pointers in an unsafe context: diff --git a/docs/csharp/reference-semantics-with-value-types.md b/docs/csharp/reference-semantics-with-value-types.md index f2d71e1705f66..02900332e18da 100644 --- a/docs/csharp/reference-semantics-with-value-types.md +++ b/docs/csharp/reference-semantics-with-value-types.md @@ -70,7 +70,7 @@ values. The `in` modifier complements `out` and `ref` in other ways as well. You cannot create overloads of a method that differ only in the presence of -`in`, `out` or `ref`. These new rules extend the same behavior that had always been +`in`, `out`, or `ref`. These new rules extend the same behavior that had always been defined for `out` and `ref` parameters. The `in` modifier may be applied to any member that takes parameters: diff --git a/docs/csharp/tour-of-csharp/structs.md b/docs/csharp/tour-of-csharp/structs.md index 9aae5ea3dc883..99c54ec623b06 100644 --- a/docs/csharp/tour-of-csharp/structs.md +++ b/docs/csharp/tour-of-csharp/structs.md @@ -34,7 +34,7 @@ With classes, it is possible for two variables to reference the same object and If `Point` is a class, the output is 20 because a and b reference the same object. If Point is a struct, the output is 10 because the assignment of `a` to `b` creates a copy of the value, and this copy is unaffected by the subsequent assignment to `a.x`. -The previous example highlights two of the limitations of structs. First, copying an entire struct is typically less efficient than copying an object reference, so assignment and value parameter passing can be more expensive with structs than with reference types. Second, except for `ref` and `out` parameters, it is not possible to create references to structs, which rules out their usage in a number of situations. +The previous example highlights two of the limitations of structs. First, copying an entire struct is typically less efficient than copying an object reference, so assignment and value parameter passing can be more expensive with structs than with reference types. Second, except for `in`, `ref`, and `out` parameters, it is not possible to create references to structs, which rules out their usage in a number of situations. >[!div class="step-by-step"] [Previous](classes-and-objects.md) diff --git a/docs/csharp/whats-new/csharp-7.md b/docs/csharp/whats-new/csharp-7.md index 3ae0baa9b5ed1..cc57be77adbb0 100644 --- a/docs/csharp/whats-new/csharp-7.md +++ b/docs/csharp/whats-new/csharp-7.md @@ -208,7 +208,7 @@ Discards are supported in the following scenarios: * When deconstructing tuples or user-defined types. -* When calling methods with [out](../language-reference/keywords/out.md) parameters. +* When calling methods with [out](../language-reference/keywords/out-parameter-modifier.md) parameters. * In a pattern matching operation with the [is](../language-reference/keywords/is.md) and [switch](../language-reference/keywords/switch.md) statements. diff --git a/samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/InParameterModifier.cs b/samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/InParameterModifier.cs new file mode 100644 index 0000000000000..9174946f30619 --- /dev/null +++ b/samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/InParameterModifier.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace InRefOutModifier +{ + public class InParameterModifier + { + + public static void Examples() + { + FirstInExample(); + + } + private static void FirstInExample() + { + // + int readonlyArgument = 44; + InArgExample(readonlyArgument); + Console.WriteLine(readonlyArgument); // value is still 44 + + void InArgExample(in int number) + { + // Uncomment the following line to see error CS8331 + //number = 19; + } + // + } + } + + // + class InOverloadExample + { + public void SampleMethod(int i) { } + public void SampleMethod(in int i) { } + } + // + +} diff --git a/samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/OutParameterModifier.cs b/samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/OutParameterModifier.cs new file mode 100644 index 0000000000000..b5d1cb4feac9c --- /dev/null +++ b/samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/OutParameterModifier.cs @@ -0,0 +1,100 @@ +using System; + +namespace InRefOutModifier +{ + public static class OutParameterModifier + { + public static void Examples() + { + FirstOutExample(); + MultipleOutParameters(); + SeparateDeclaration(); + DeclareAtCallsite(); + UseImplicitOutArgument(); + } + + private static void FirstOutExample() + { + // + int initializeInMethod; + OutArgExample(out initializeInMethod); + Console.WriteLine(initializeInMethod); // value is now 44 + + void OutArgExample(out int number) + { + number = 44; + } + // + } + + private static void MultipleOutParameters() + { + // + void Method(out int answer, out string message, out string stillNull) + { + answer = 44; + message = "I've been returned"; + stillNull = null; + } + + int argNumber; + string argMessage, argDefault; + Method(out argNumber, out argMessage, out argDefault); + Console.WriteLine(argNumber); + Console.WriteLine(argMessage); + Console.WriteLine(argDefault == null); + // + } + + private static void SeparateDeclaration() + { + // + string numberAsString = "1640"; + + int number; + if (Int32.TryParse(numberAsString, out number)) + Console.WriteLine($"Converted '{numberAsString}' to {number}"); + else + Console.WriteLine($"Unable to convert '{numberAsString}'"); + // The example displays the following output: + // Converted '1640' to 1640 + // + } + + private static void DeclareAtCallsite() + { + // + string numberAsString = "1640"; + + if (Int32.TryParse(numberAsString, out int number)) + Console.WriteLine($"Converted '{numberAsString}' to {number}"); + else + Console.WriteLine($"Unable to convert '{numberAsString}'"); + // The example displays the following output: + // Converted '1640' to 1640 + // + } + + private static void UseImplicitOutArgument() + { + // + string numberAsString = "1640"; + + if (Int32.TryParse(numberAsString, out var number)) + Console.WriteLine($"Converted '{numberAsString}' to {number}"); + else + Console.WriteLine($"Unable to convert '{numberAsString}'"); + // The example displays the following output: + // Converted '1640' to 1640 + // + } + } + + // + class OutOverloadExample + { + public void SampleMethod(int i) { } + public void SampleMethod(out int i) => i = 5; + } + // +} diff --git a/samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/Program.cs b/samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/Program.cs new file mode 100644 index 0000000000000..1b4d54987e005 --- /dev/null +++ b/samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/Program.cs @@ -0,0 +1,14 @@ +using System; + +namespace InRefOutModifier +{ + class Program + { + static void Main(string[] args) + { + InParameterModifier.Examples(); + OutParameterModifier.Examples(); + RefParameterModifier.Examples(); + } + } +} diff --git a/samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/RefParameterModifier.cs b/samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/RefParameterModifier.cs new file mode 100644 index 0000000000000..7ec9ab8305081 --- /dev/null +++ b/samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/RefParameterModifier.cs @@ -0,0 +1,131 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace InRefOutModifier +{ + public class RefParameterModifier + { + public static void Examples() + { + FirstRefExample(); + ModifyProductsByReference(); + } + + private static void FirstRefExample() + { + // + void Method(ref int refArgument) + { + refArgument = refArgument + 44; + } + + int number = 1; + Method(ref number); + Console.WriteLine(number); + // Output: 45 + // + } + + // + class Product + { + public Product(string name, int newID) + { + ItemName = name; + ItemID = newID; + } + + public string ItemName { get; set; } + public int ItemID { get; set; } + } + + private static void ModifyProductsByReference() + { + // Declare an instance of Product and display its initial values. + Product item = new Product("Fasteners", 54321); + System.Console.WriteLine("Original values in Main. Name: {0}, ID: {1}\n", + item.ItemName, item.ItemID); + + // Pass the product instance to ChangeByReference. + ChangeByReference(ref item); + System.Console.WriteLine("Back in Main. Name: {0}, ID: {1}\n", + item.ItemName, item.ItemID); + } + + private static void ChangeByReference(ref Product itemRef) + { + // Change the address that is stored in the itemRef parameter. + itemRef = new Product("Stapler", 99999); + + // You can change the value of one of the properties of + // itemRef. The change happens to item in Main as well. + itemRef.ItemID = 12345; + } + // + + private static void BookCollectionExample() + { + // + var bc = new BookCollection(); + bc.ListBooks(); + + ref var book = ref bc.GetBookByTitle("Call of the Wild, The"); + if (book != null) + book = new Book { Title = "Republic, The", Author = "Plato" }; + bc.ListBooks(); + // The example displays the following output: + // Call of the Wild, The, by Jack London + // Tale of Two Cities, A, by Charles Dickens + // + // Republic, The, by Plato + // Tale of Two Cities, A, by Charles Dickens + // + + } + + } + // + class RefOverloadExample + { + public void SampleMethod(int i) { } + public void SampleMethod(ref int i) { } + } + // + + // + + public class Book + { + public string Author; + public string Title; + } + + public class BookCollection + { + private Book[] books = { new Book { Title = "Call of the Wild, The", Author = "Jack London" }, + new Book { Title = "Tale of Two Cities, A", Author = "Charles Dickens" } + }; + private Book nobook = null; + + public ref Book GetBookByTitle(string title) + { + for (int ctr = 0; ctr < books.Length; ctr++) + { + if (title == books[ctr].Title) + return ref books[ctr]; + } + return ref nobook; + } + + public void ListBooks() + { + foreach (var book in books) + { + Console.WriteLine($"{book.Title}, by {book.Author}"); + } + Console.WriteLine(); + } + } + // +} diff --git a/samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/in-ref-out-modifier.csproj b/samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/in-ref-out-modifier.csproj new file mode 100644 index 0000000000000..b39557a3d3303 --- /dev/null +++ b/samples/snippets/csharp/language-reference/keywords/in-ref-out-modifier/in-ref-out-modifier.csproj @@ -0,0 +1,13 @@ + + + + Exe + netcoreapp2.0 + InRefOutModifier + + + + latest + + + diff --git a/samples/snippets/csharp/language-reference/keywords/out/out-1.cs b/samples/snippets/csharp/language-reference/keywords/out/out-1.cs deleted file mode 100644 index 33e6c5575024e..0000000000000 --- a/samples/snippets/csharp/language-reference/keywords/out/out-1.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; - -class OutExample -{ - static void Method(out int i) - { - i = 44; - } - - static void Main() - { - int value; - Method(out value); - Console.WriteLine(value); // value is now 44 - } -} diff --git a/samples/snippets/csharp/language-reference/keywords/out/out-3.cs b/samples/snippets/csharp/language-reference/keywords/out/out-3.cs deleted file mode 100644 index e6f3a79e9089e..0000000000000 --- a/samples/snippets/csharp/language-reference/keywords/out/out-3.cs +++ /dev/null @@ -1,5 +0,0 @@ - class OutOverloadExample - { - public void SampleMethod(int i) { } - public void SampleMethod(out int i) { i = 5; } - } \ No newline at end of file diff --git a/samples/snippets/csharp/language-reference/keywords/out/out-4.cs b/samples/snippets/csharp/language-reference/keywords/out/out-4.cs deleted file mode 100644 index 848c99f97404b..0000000000000 --- a/samples/snippets/csharp/language-reference/keywords/out/out-4.cs +++ /dev/null @@ -1,19 +0,0 @@ -class OutReturnExample -{ - static void Method(out int i, out string s1, out string s2) - { - i = 44; - s1 = "I've been returned"; - s2 = null; - } - - static void Main() - { - int value; - string str1, str2; - Method(out value, out str1, out str2); - // value is now 44 - // str1 is now "I've been returned" - // str2 is (still) null; - } -} diff --git a/samples/snippets/csharp/language-reference/keywords/out/out-5.cs b/samples/snippets/csharp/language-reference/keywords/out/out-5.cs deleted file mode 100644 index 260970f6b8cfd..0000000000000 --- a/samples/snippets/csharp/language-reference/keywords/out/out-5.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; - -public class Example -{ - public static void Main() - { - string value = "1640"; - - int number; - if (Int32.TryParse(value, out number)) - Console.WriteLine($"Converted '{value}' to {number}"); - else - Console.WriteLine($"Unable to convert '{value}'"); - } -} -// The example displays the following output: -// Converted '1640' to 1640 - - diff --git a/samples/snippets/csharp/language-reference/keywords/out/out-6.cs b/samples/snippets/csharp/language-reference/keywords/out/out-6.cs deleted file mode 100644 index 8fda035888c37..0000000000000 --- a/samples/snippets/csharp/language-reference/keywords/out/out-6.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; - -public class Example -{ - public static void Main() - { - string value = "1640"; - - if (Int32.TryParse(value, out int number)) - Console.WriteLine($"Converted '{value}' to {number}"); - else - Console.WriteLine($"Unable to convert '{value}'"); - } -} -// The example displays the following output: -// Converted '1640' to 1640 - - diff --git a/samples/snippets/csharp/language-reference/keywords/out/out-7.cs b/samples/snippets/csharp/language-reference/keywords/out/out-7.cs deleted file mode 100644 index a2b1e8b883c11..0000000000000 --- a/samples/snippets/csharp/language-reference/keywords/out/out-7.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; - -public class Example -{ - public static void Main() - { - string value = "1640"; - - if (Int32.TryParse(value, out var number)) - Console.WriteLine($"Converted '{value}' to {number}"); - else - Console.WriteLine($"Unable to convert '{value}'"); - } -} -// The example displays the following output: -// Converted '1640' to 1640 - - diff --git a/samples/snippets/csharp/language-reference/keywords/ref/ref-1.cs b/samples/snippets/csharp/language-reference/keywords/ref/ref-1.cs deleted file mode 100644 index b1d83e8b1d2d5..0000000000000 --- a/samples/snippets/csharp/language-reference/keywords/ref/ref-1.cs +++ /dev/null @@ -1,15 +0,0 @@ -class RefExample -{ - static void Method(ref int i) - { - i = i + 44; - } - - static void Main() - { - int val = 1; - Method(ref val); - Console.WriteLine(val); - // Output: 45 - } -} diff --git a/samples/snippets/csharp/language-reference/keywords/ref/ref-2.cs b/samples/snippets/csharp/language-reference/keywords/ref/ref-2.cs deleted file mode 100644 index a1770da205357..0000000000000 --- a/samples/snippets/csharp/language-reference/keywords/ref/ref-2.cs +++ /dev/null @@ -1,7 +0,0 @@ - class CS0663_Example - { - // Compiler error CS0663: "Cannot define overloaded - // methods that differ only on ref and out". - public void SampleMethod(out int i) { } - public void SampleMethod(ref int i) { } - } \ No newline at end of file diff --git a/samples/snippets/csharp/language-reference/keywords/ref/ref-3.cs b/samples/snippets/csharp/language-reference/keywords/ref/ref-3.cs deleted file mode 100644 index 3a40464d69661..0000000000000 --- a/samples/snippets/csharp/language-reference/keywords/ref/ref-3.cs +++ /dev/null @@ -1,5 +0,0 @@ - class RefOverloadExample - { - public void SampleMethod(int i) { } - public void SampleMethod(ref int i) { } - } diff --git a/samples/snippets/csharp/language-reference/keywords/ref/ref-4.cs b/samples/snippets/csharp/language-reference/keywords/ref/ref-4.cs deleted file mode 100644 index 97acc021f30ef..0000000000000 --- a/samples/snippets/csharp/language-reference/keywords/ref/ref-4.cs +++ /dev/null @@ -1,41 +0,0 @@ -class RefExample2 -{ - static void Main() - { - // Declare an instance of Product and display its initial values. - Product item = new Product("Fasteners", 54321); - System.Console.WriteLine("Original values in Main. Name: {0}, ID: {1}\n", - item.ItemName, item.ItemID); - - // Pass the product instance to ChangeByReference. - ChangeByReference(ref item); - System.Console.WriteLine("Back in Main. Name: {0}, ID: {1}\n", - item.ItemName, item.ItemID); - } - - static void ChangeByReference(ref Product itemRef) - { - // Change the address that is stored in the itemRef parameter. - itemRef = new Product("Stapler", 99999); - - // You can change the value of one of the properties of - // itemRef. The change happens to item in Main as well. - itemRef.ItemID = 12345; - } -} - -class Product -{ - public Product(string name, int newID) - { - ItemName = name; - ItemID = newID; - } - - public string ItemName { get; set; } - public int ItemID { get; set; } -} -// Output: -// Original values in Main. Name: Fasteners, ID: 54321 -// -// Back in Main. Name: Stapler, ID: 12345 diff --git a/samples/snippets/csharp/language-reference/keywords/ref/ref-5.cs b/samples/snippets/csharp/language-reference/keywords/ref/ref-5.cs deleted file mode 100644 index 796edfc5d539e..0000000000000 --- a/samples/snippets/csharp/language-reference/keywords/ref/ref-5.cs +++ /dev/null @@ -1,63 +0,0 @@ -// -using System; - -public class Book -{ - public string Author; - public string Title; -} - -public class BookCollection -{ - private Book[] books = { new Book { Title = "Call of the Wild, The", Author = "Jack London" }, - new Book { Title = "Tale of Two Cities, A", Author = "Charles Dickens" } - }; - private Book nobook = null; - - public ref Book GetBookByTitle(string title) - { - for (int ctr = 0; ctr < books.Length; ctr++) - { - if (title == books[ctr].Title) - return ref books[ctr]; - } - return ref nobook; - } - - public void ListBooks() - { - foreach (var book in books) { - Console.WriteLine($"{book.Title}, by {book.Author}"); - } - Console.WriteLine(); - } -} -// - -namespace Caller -{ -// -using System; - -class Example -{ - static void Main() - { - var bc = new BookCollection(); - bc.ListBooks(); - - ref var book = ref bc.GetBookByTitle("Call of the Wild, The"); - if (book != null) - book = new Book { Title = "Republic, The", Author = "Plato" }; - bc.ListBooks(); - } -} -// The example displays the following output: -// Call of the Wild, The, by Jack London -// Tale of Two Cities, A, by Charles Dickens -// -// Republic, The, by Plato -// Tale of Two Cities, A, by Charles Dickens -// -} -