Recall & Review
beginner
What happens when you pass a reference type to a method in C#?
The method receives a copy of the reference, so it can modify the object's data, but reassigning the parameter inside the method won't affect the original reference outside.
Click to reveal answer
intermediate
How can you make a method change the reference itself (not just the object's data) in C#?
Use the
ref keyword in the method parameter and when calling the method. This passes the reference by reference, allowing the method to reassign the original reference.Click to reveal answer
beginner
True or False: Passing a reference type to a method always allows the method to change the original object.
True. The method can change the object's data because it has a reference to the same object in memory.
Click to reveal answer
intermediate
What is the difference between passing a reference type normally and passing it with
ref in C#?Normally, the method gets a copy of the reference and can change the object's data but not the reference itself. With
ref, the method can change the reference to point to a new object, affecting the caller's variable.Click to reveal answer
beginner
Consider this code snippet:<br><pre>class Person { public string Name; }<br>void ChangeName(Person p) { p.Name = "Alice"; }<br>Person person = new Person { Name = "Bob" };<br>ChangeName(person);<br>What is the value of <code>person.Name</code> after calling <code>ChangeName</code>?</pre>The value is "Alice" because the method changed the data inside the object that
person references.Click to reveal answer
When you pass a reference type to a method without
ref, what can the method change?✗ Incorrect
The method receives a copy of the reference, so it can modify the object's data but cannot change the caller's reference to a new object.
How do you allow a method to change the reference variable itself in C#?
✗ Incorrect
The
ref keyword passes the reference by reference, allowing the method to reassign the caller's variable.If a method changes a property of a passed reference type, will the change be visible outside the method?
✗ Incorrect
The method works on the same object in memory, so changes to its data are visible outside.
What does the
ref keyword do when used with a reference type parameter?✗ Incorrect
ref passes the reference by reference, so the method can change which object the caller's variable points to.Which statement is true about passing reference types to methods?
✗ Incorrect
Passing a reference type copies the reference (like an address), not the whole object.
Explain what happens when you pass a reference type to a method in C# and how changes inside the method affect the original object.
Think about the difference between the reference and the object it points to.
You got /4 concepts.
Describe how the
ref keyword changes the behavior of passing reference types to methods in C#.Consider what happens if the method assigns a new object to the parameter.
You got /3 concepts.