From aba007cceffb5bccea5a719665ee379ecd4ba223 Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Thu, 18 Feb 2021 15:58:58 +1100 Subject: [PATCH] Finished Testing Object Reference & Referencing Different Object & Passing Parameter by Refernce --- ...ng_with_reference_types_and_value_types.md | 54 ++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/csharp_fundamentals/5_working_with_reference_types_and_value_types.md b/csharp_fundamentals/5_working_with_reference_types_and_value_types.md index 1ca7519..de90bb7 100644 --- a/csharp_fundamentals/5_working_with_reference_types_and_value_types.md +++ b/csharp_fundamentals/5_working_with_reference_types_and_value_types.md @@ -21,11 +21,61 @@ Steps: Convention of naming: * Public class member beginning with UpperCase -## Testing Object References & Referencing Different Object +## Testing Object References & Referencing Different Object & Passing Parameters by Reference Two reference type variable can reference 2 different or same object. Check tests `GetBookReturnDifferentObject()` & `TwoVarsCanReferenceSameObject()` in `TypeTests.cs` Trick: -* `Object.ReferenceEquals(obj1, obj2)` return Boolean for whether two objs has same reference value \ No newline at end of file +* `Object.ReferenceEquals(obj1, obj2)` return Boolean for whether two objs has same reference value + +## Passing Parameters by Value & Returning Object References + +Reference: +* [Passing Parameter (C# Programming Guide)](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/passing-parameters) + +**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 effect on the original data stored in the argument variable. If you want the called method to change the value of the argument, you must pass it by reference, using the ref or out keyword. + +```csharp + [Fact] + public void CSharpIsPassByValue() + { + var book1 = GetBook("Book1"); + GetBookSetName(book1, "New Name"); + + Assert.Equal(book1.Name, "New Name"); + } + + private void GetBookSetName(Book book, string name) + { + book = new Book(name); + book.Name = name; + } +``` + +As shown in above: +1. `boo1` is pass-by-value into function (i.e. its value, reference to object is copied into function) +2. In `GetBookSetName` function, that value (reference to "Book1" object) is replaced with a new object. +3. `book.Name = name` only update name within function. Nothing reflect on object itself. + +To **Pass-parameter-by-reference**, use `ref/out param` in both function definition and invoking + +```csharp + [Fact] + public void CSharpCanPassByReference() + { + var book1 = GetBook("Book1"); + GetBookSetName(ref book1, "New Name"); + + Assert.Equal(book1.Name, "New Name"); + } + + private void GetBookSetName(ref Book book, string name) + { + book = new Book(name); + book.Name = name; + } +``` + +Not very often use. \ No newline at end of file